
<span id="id1"></span><h1><span class="yiyi-st" id="yiyi-84">Cookbook</span></h1>
        <blockquote>
        <p>原文：<a href="http://pandas.pydata.org/pandas-docs/stable/cookbook.html">http://pandas.pydata.org/pandas-docs/stable/cookbook.html</a></p>
        <p>译者：<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
        <p>校对：（虚位以待）</p>
        </blockquote>
    
<p><span class="yiyi-st" id="yiyi-85">这里有大量的例程和链接，就像食谱中 <em>简单而可口</em> 示例一样，希望能对您学习pandas库有用。</span><span class="yiyi-st" id="yiyi-86">我们鼓励用户为此文档添加内容。</span></p>
<p><span class="yiyi-st" id="yiyi-87">如果您向此部分中添加链接或者例子，我们会把它当做 <em>First Pull Request</em> 处理。</span></p>
<p><span class="yiyi-st" id="yiyi-88">我们贴在此处的示例大多都是经过简化、凝练的，对新用户友好。这些例子可能已经在Stack-Overflow进行了讨论或者已经出现在了GitHub链接中。</span><span class="yiyi-st" id="yiyi-89">那些链接包含有更多的信息，这里的例程并没有扩展讲解。</span></p>
<p><span class="yiyi-st" id="yiyi-90">为了照顾初学者，在导入的模块中只有Pandas（pd）和Numpy（np）进行了缩写，</span><span class="yiyi-st" id="yiyi-91">其余的模块导入时都保留了原来的名称。</span></p>
<p><span class="yiyi-st" id="yiyi-92">这些例子是为python 3.4编写的。</span><span class="yiyi-st" id="yiyi-93">较早的python版本或许需要修改一下。</span></p>
<div class="section" id="idioms">
<h2><span class="yiyi-st" id="yiyi-94">Idioms</span></h2>
<p id="cookbook-idioms"><span class="yiyi-st" id="yiyi-95">这里有一些整齐优雅的pandas<code class="docutils literal"><span class="pre">惯用写法</span></code></span></p>
<p><span class="yiyi-st" id="yiyi-96"><a class="reference external" href="http://stackoverflow.com/questions/17128302/python-pandas-idiom-for-if-then-else">使用if-then / if-then-else选定某一列中某些值，但是却修改这些值所在行的其他一列或若干列中的值：</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span>
<span class="gp">   ...:</span>      <span class="p">{</span><span class="s1">&apos;AAA&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">],</span> <span class="s1">&apos;BBB&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span><span class="mi">20</span><span class="p">,</span><span class="mi">30</span><span class="p">,</span><span class="mi">40</span><span class="p">],</span><span class="s1">&apos;CCC&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">100</span><span class="p">,</span><span class="mi">50</span><span class="p">,</span><span class="o">-</span><span class="mi">30</span><span class="p">,</span><span class="o">-</span><span class="mi">50</span><span class="p">]});</span> <span class="n">df</span>
<span class="gp">   ...:</span> 
<span class="gr">Out[1]: </span>
<span class="go">   AAA  BBB  CCC</span>
<span class="go">0    4   10  100</span>
<span class="go">1    5   20   50</span>
<span class="go">2    6   30  -30</span>
<span class="go">3    7   40  -50</span>
</pre></div>
</div>
<div class="section" id="if-then">
<h3><span class="yiyi-st" id="yiyi-97">if-then...</span></h3>
<p><span class="yiyi-st" id="yiyi-98">if-then 选中修改一列的值</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [2]: </span><span class="n">df</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="n">df</span><span class="o">.</span><span class="n">AAA</span> <span class="o">&gt;=</span> <span class="mi">5</span><span class="p">,</span><span class="s1">&apos;BBB&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="n">df</span>
<span class="gr">Out[2]: </span>
<span class="go">   AAA  BBB  CCC</span>
<span class="go">0    4   10  100</span>
<span class="go">1    5   -1   50</span>
<span class="go">2    6   -1  -30</span>
<span class="go">3    7   -1  -50</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-99">if-then 选中修改2列的值</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [3]: </span><span class="n">df</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="n">df</span><span class="o">.</span><span class="n">AAA</span> <span class="o">&gt;=</span> <span class="mi">5</span><span class="p">,[</span><span class="s1">&apos;BBB&apos;</span><span class="p">,</span><span class="s1">&apos;CCC&apos;</span><span class="p">]]</span> <span class="o">=</span> <span class="mi">555</span><span class="p">;</span> <span class="n">df</span>
<span class="gr">Out[3]: </span>
<span class="go">   AAA  BBB  CCC</span>
<span class="go">0    4   10  100</span>
<span class="go">1    5  555  555</span>
<span class="go">2    6  555  555</span>
<span class="go">3    7  555  555</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-100">此例与上一例逻辑相反，用来实现 else</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [4]: </span><span class="n">df</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="n">df</span><span class="o">.</span><span class="n">AAA</span> <span class="o">&lt;</span> <span class="mi">5</span><span class="p">,[</span><span class="s1">&apos;BBB&apos;</span><span class="p">,</span><span class="s1">&apos;CCC&apos;</span><span class="p">]]</span> <span class="o">=</span> <span class="mi">2000</span><span class="p">;</span> <span class="n">df</span>
<span class="gr">Out[4]: </span>
<span class="go">   AAA   BBB   CCC</span>
<span class="go">0    4  2000  2000</span>
<span class="go">1    5   555   555</span>
<span class="go">2    6   555   555</span>
<span class="go">3    7   555   555</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-101">或者你也可以使用mask</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [5]: </span><span class="n">df_mask</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;AAA&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="bp">True</span><span class="p">]</span> <span class="o">*</span> <span class="mi">4</span><span class="p">,</span> <span class="s1">&apos;BBB&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="bp">False</span><span class="p">]</span> <span class="o">*</span> <span class="mi">4</span><span class="p">,</span><span class="s1">&apos;CCC&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="bp">True</span><span class="p">,</span><span class="bp">False</span><span class="p">]</span> <span class="o">*</span> <span class="mi">2</span><span class="p">})</span>

<span class="gp">In [6]: </span><span class="n">df</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">df_mask</span><span class="p">,</span><span class="o">-</span><span class="mi">1000</span><span class="p">)</span>
<span class="gr">Out[6]: </span>
<span class="go">   AAA   BBB   CCC</span>
<span class="go">0    4 -1000  2000</span>
<span class="go">1    5 -1000 -1000</span>
<span class="go">2    6 -1000   555</span>
<span class="go">3    7 -1000 -1000</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-102"><a class="reference external" href="http://stackoverflow.com/questions/19913659/pandas-conditional-creation-of-a-series-dataframe-column">通过numpy&apos;s where()，实现if-then-else的逻辑</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [7]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span>
<span class="gp">   ...:</span>      <span class="p">{</span><span class="s1">&apos;AAA&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">],</span> <span class="s1">&apos;BBB&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span><span class="mi">20</span><span class="p">,</span><span class="mi">30</span><span class="p">,</span><span class="mi">40</span><span class="p">],</span><span class="s1">&apos;CCC&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">100</span><span class="p">,</span><span class="mi">50</span><span class="p">,</span><span class="o">-</span><span class="mi">30</span><span class="p">,</span><span class="o">-</span><span class="mi">50</span><span class="p">]});</span> <span class="n">df</span>
<span class="gp">   ...:</span> 
<span class="gr">Out[7]: </span>
<span class="go">   AAA  BBB  CCC</span>
<span class="go">0    4   10  100</span>
<span class="go">1    5   20   50</span>
<span class="go">2    6   30  -30</span>
<span class="go">3    7   40  -50</span>

<span class="gp">In [8]: </span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;logic&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;AAA&apos;</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">5</span><span class="p">,</span><span class="s1">&apos;high&apos;</span><span class="p">,</span><span class="s1">&apos;low&apos;</span><span class="p">);</span> <span class="n">df</span>
<span class="gr">Out[8]: </span>
<span class="go">   AAA  BBB  CCC logic</span>
<span class="go">0    4   10  100   low</span>
<span class="go">1    5   20   50   low</span>
<span class="go">2    6   30  -30  high</span>
<span class="go">3    7   40  -50  high</span>
</pre></div>
</div>
</div>
<div class="section" id="splitting">
<h3><span class="yiyi-st" id="yiyi-103">Splitting</span></h3>
<p><span class="yiyi-st" id="yiyi-104"><a class="reference external" href="http://stackoverflow.com/questions/14957116/how-to-split-a-dataframe-according-to-a-boolean-criterion">使用布尔值标准选择数据</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [9]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span>
<span class="gp">   ...:</span>      <span class="p">{</span><span class="s1">&apos;AAA&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">],</span> <span class="s1">&apos;BBB&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span><span class="mi">20</span><span class="p">,</span><span class="mi">30</span><span class="p">,</span><span class="mi">40</span><span class="p">],</span><span class="s1">&apos;CCC&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">100</span><span class="p">,</span><span class="mi">50</span><span class="p">,</span><span class="o">-</span><span class="mi">30</span><span class="p">,</span><span class="o">-</span><span class="mi">50</span><span class="p">]});</span> <span class="n">df</span>
<span class="gp">   ...:</span> 
<span class="gr">Out[9]: </span>
<span class="go">   AAA  BBB  CCC</span>
<span class="go">0    4   10  100</span>
<span class="go">1    5   20   50</span>
<span class="go">2    6   30  -30</span>
<span class="go">3    7   40  -50</span>

<span class="gp">In [10]: </span><span class="n">dflow</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="n">df</span><span class="o">.</span><span class="n">AAA</span> <span class="o">&lt;=</span> <span class="mi">5</span><span class="p">]</span>

<span class="gp">In [11]: </span><span class="n">dfhigh</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="n">df</span><span class="o">.</span><span class="n">AAA</span> <span class="o">&gt;</span> <span class="mi">5</span><span class="p">]</span>

<span class="gp">In [12]: </span><span class="n">dflow</span><span class="p">;</span> <span class="n">dfhigh</span>
<span class="gr">Out[12]: </span>
<span class="go">   AAA  BBB  CCC</span>
<span class="go">2    6   30  -30</span>
<span class="go">3    7   40  -50</span>
</pre></div>
</div>
</div>
<div class="section" id="building-criteria">
<h3><span class="yiyi-st" id="yiyi-105">Building Criteria</span></h3>
<p><span class="yiyi-st" id="yiyi-106"><a class="reference external" href="http://stackoverflow.com/questions/15315452/selecting-with-complex-criteria-from-pandas-dataframe">根据多列的值选择</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [13]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span>
<span class="gp">   ....:</span>      <span class="p">{</span><span class="s1">&apos;AAA&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">],</span> <span class="s1">&apos;BBB&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span><span class="mi">20</span><span class="p">,</span><span class="mi">30</span><span class="p">,</span><span class="mi">40</span><span class="p">],</span><span class="s1">&apos;CCC&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">100</span><span class="p">,</span><span class="mi">50</span><span class="p">,</span><span class="o">-</span><span class="mi">30</span><span class="p">,</span><span class="o">-</span><span class="mi">50</span><span class="p">]});</span> <span class="n">df</span>
<span class="gp">   ....:</span> 
<span class="gr">Out[13]: </span>
<span class="go">   AAA  BBB  CCC</span>
<span class="go">0    4   10  100</span>
<span class="go">1    5   20   50</span>
<span class="go">2    6   30  -30</span>
<span class="go">3    7   40  -50</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-107">...实现‘与’操作（没有更改原内容而是返回了一个新的Series）</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [14]: </span><span class="n">newseries</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">loc</span><span class="p">[(</span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;BBB&apos;</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mi">25</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;CCC&apos;</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="o">-</span><span class="mi">40</span><span class="p">),</span> <span class="s1">&apos;AAA&apos;</span><span class="p">];</span> <span class="n">newseries</span>
<span class="gr">Out[14]: </span>
<span class="go">0    4</span>
<span class="go">1    5</span>
<span class="go">Name: AAA, dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-108">...实现“或”操作（没有更改原内容而是返回了一个新的Series）</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [15]: </span><span class="n">newseries</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">loc</span><span class="p">[(</span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;BBB&apos;</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">25</span><span class="p">)</span> <span class="o">|</span> <span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;CCC&apos;</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="o">-</span><span class="mi">40</span><span class="p">),</span> <span class="s1">&apos;AAA&apos;</span><span class="p">];</span> <span class="n">newseries</span><span class="p">;</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-109">...实现“或”操作（直接修改了DataFrame的内容）</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [16]: </span><span class="n">df</span><span class="o">.</span><span class="n">loc</span><span class="p">[(</span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;BBB&apos;</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">25</span><span class="p">)</span> <span class="o">|</span> <span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;CCC&apos;</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="mi">75</span><span class="p">),</span> <span class="s1">&apos;AAA&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.1</span><span class="p">;</span> <span class="n">df</span>
<span class="gr">Out[16]: </span>
<span class="go">   AAA  BBB  CCC</span>
<span class="go">0  0.1   10  100</span>
<span class="go">1  5.0   20   50</span>
<span class="go">2  0.1   30  -30</span>
<span class="go">3  0.1   40  -50</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-110"><a class="reference external" href="http://stackoverflow.com/questions/17758023/return-rows-in-a-dataframe-closest-to-a-user-defined-number">使用argsort</a>选择数据最接近某个值的行</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [17]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span>
<span class="gp">   ....:</span>      <span class="p">{</span><span class="s1">&apos;AAA&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">],</span> <span class="s1">&apos;BBB&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span><span class="mi">20</span><span class="p">,</span><span class="mi">30</span><span class="p">,</span><span class="mi">40</span><span class="p">],</span><span class="s1">&apos;CCC&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">100</span><span class="p">,</span><span class="mi">50</span><span class="p">,</span><span class="o">-</span><span class="mi">30</span><span class="p">,</span><span class="o">-</span><span class="mi">50</span><span class="p">]});</span> <span class="n">df</span>
<span class="gp">   ....:</span> 
<span class="gr">Out[17]: </span>
<span class="go">   AAA  BBB  CCC</span>
<span class="go">0    4   10  100</span>
<span class="go">1    5   20   50</span>
<span class="go">2    6   30  -30</span>
<span class="go">3    7   40  -50</span>

<span class="gp">In [18]: </span><span class="n">aValue</span> <span class="o">=</span> <span class="mf">43.0</span>

<span class="gp">In [19]: </span><span class="n">df</span><span class="o">.</span><span class="n">ix</span><span class="p">[(</span><span class="n">df</span><span class="o">.</span><span class="n">CCC</span><span class="o">-</span><span class="n">aValue</span><span class="p">)</span><span class="o">.</span><span class="n">abs</span><span class="p">()</span><span class="o">.</span><span class="n">argsort</span><span class="p">()]</span>
<span class="gr">Out[19]: </span>
<span class="go">   AAA  BBB  CCC</span>
<span class="go">1    5   20   50</span>
<span class="go">0    4   10  100</span>
<span class="go">2    6   30  -30</span>
<span class="go">3    7   40  -50</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-111"><a class="reference external" href="http://stackoverflow.com/questions/21058254/pandas-boolean-operation-in-a-python-list/21058331">使用二元运算符</a>动态地减少条件列表</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [20]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span>
<span class="gp">   ....:</span>      <span class="p">{</span><span class="s1">&apos;AAA&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">],</span> <span class="s1">&apos;BBB&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span><span class="mi">20</span><span class="p">,</span><span class="mi">30</span><span class="p">,</span><span class="mi">40</span><span class="p">],</span><span class="s1">&apos;CCC&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">100</span><span class="p">,</span><span class="mi">50</span><span class="p">,</span><span class="o">-</span><span class="mi">30</span><span class="p">,</span><span class="o">-</span><span class="mi">50</span><span class="p">]});</span> <span class="n">df</span>
<span class="gp">   ....:</span> 
<span class="gr">Out[20]: </span>
<span class="go">   AAA  BBB  CCC</span>
<span class="go">0    4   10  100</span>
<span class="go">1    5   20   50</span>
<span class="go">2    6   30  -30</span>
<span class="go">3    7   40  -50</span>

<span class="gp">In [21]: </span><span class="n">Crit1</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">AAA</span> <span class="o">&lt;=</span> <span class="mf">5.5</span>

<span class="gp">In [22]: </span><span class="n">Crit2</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">BBB</span> <span class="o">==</span> <span class="mf">10.0</span>

<span class="gp">In [23]: </span><span class="n">Crit3</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">CCC</span> <span class="o">&gt;</span> <span class="o">-</span><span class="mf">40.0</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-112">可以硬编码：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [24]: </span><span class="n">AllCrit</span> <span class="o">=</span> <span class="n">Crit1</span> <span class="o">&amp;</span> <span class="n">Crit2</span> <span class="o">&amp;</span> <span class="n">Crit3</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-113">...或者可以使用动态构建的标准列表</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [25]: </span><span class="n">CritList</span> <span class="o">=</span> <span class="p">[</span><span class="n">Crit1</span><span class="p">,</span><span class="n">Crit2</span><span class="p">,</span><span class="n">Crit3</span><span class="p">]</span>

<span class="gp">In [26]: </span><span class="n">AllCrit</span> <span class="o">=</span> <span class="n">functools</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">:</span> <span class="n">x</span> <span class="o">&amp;</span> <span class="n">y</span><span class="p">,</span> <span class="n">CritList</span><span class="p">)</span>

<span class="gp">In [27]: </span><span class="n">df</span><span class="p">[</span><span class="n">AllCrit</span><span class="p">]</span>
<span class="gr">Out[27]: </span>
<span class="go">   AAA  BBB  CCC</span>
<span class="go">0    4   10  100</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="selection">
<span id="cookbook-selection"></span><h2><span class="yiyi-st" id="yiyi-114">Selection</span></h2>
<div class="section" id="dataframes">
<h3><span class="yiyi-st" id="yiyi-115">DataFrames</span></h3>
<p><span class="yiyi-st" id="yiyi-116"><a class="reference internal" href="indexing.html#indexing"><span class="std std-ref">indexing</span></a>文档。</span></p>
<p><span class="yiyi-st" id="yiyi-117"><a class="reference external" href="http://stackoverflow.com/questions/14725068/pandas-using-row-labels-in-boolean-indexing">使用行标签和值条件</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [28]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span>
<span class="gp">   ....:</span>      <span class="p">{</span><span class="s1">&apos;AAA&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">],</span> <span class="s1">&apos;BBB&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span><span class="mi">20</span><span class="p">,</span><span class="mi">30</span><span class="p">,</span><span class="mi">40</span><span class="p">],</span><span class="s1">&apos;CCC&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">100</span><span class="p">,</span><span class="mi">50</span><span class="p">,</span><span class="o">-</span><span class="mi">30</span><span class="p">,</span><span class="o">-</span><span class="mi">50</span><span class="p">]});</span> <span class="n">df</span>
<span class="gp">   ....:</span> 
<span class="gr">Out[28]: </span>
<span class="go">   AAA  BBB  CCC</span>
<span class="go">0    4   10  100</span>
<span class="go">1    5   20   50</span>
<span class="go">2    6   30  -30</span>
<span class="go">3    7   40  -50</span>

<span class="gp">In [29]: </span><span class="n">df</span><span class="p">[(</span><span class="n">df</span><span class="o">.</span><span class="n">AAA</span> <span class="o">&lt;=</span> <span class="mi">6</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">isin</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">4</span><span class="p">]))]</span>
<span class="gr">Out[29]: </span>
<span class="go">   AAA  BBB  CCC</span>
<span class="go">0    4   10  100</span>
<span class="go">2    6   30  -30</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-118"><a class="reference external" href="https://github.com/pandas-dev/pandas/issues/2904">使用loc进行面向标签切片和iloc位置切片</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [30]: </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&apos;AAA&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">],</span> <span class="s1">&apos;BBB&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span><span class="mi">20</span><span class="p">,</span><span class="mi">30</span><span class="p">,</span><span class="mi">40</span><span class="p">],</span><span class="s1">&apos;CCC&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">100</span><span class="p">,</span><span class="mi">50</span><span class="p">,</span><span class="o">-</span><span class="mi">30</span><span class="p">,</span><span class="o">-</span><span class="mi">50</span><span class="p">]}</span>

<span class="gp">In [31]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">data</span><span class="p">,</span><span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;foo&apos;</span><span class="p">,</span><span class="s1">&apos;bar&apos;</span><span class="p">,</span><span class="s1">&apos;boo&apos;</span><span class="p">,</span><span class="s1">&apos;kar&apos;</span><span class="p">]);</span> <span class="n">df</span>
<span class="gr">Out[31]: </span>
<span class="go">     AAA  BBB  CCC</span>
<span class="go">foo    4   10  100</span>
<span class="go">bar    5   20   50</span>
<span class="go">boo    6   30  -30</span>
<span class="go">kar    7   40  -50</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-119">有2个显式切片方法，第三个一般情况</span></p>
<ol class="arabic simple">
<li><span class="yiyi-st" id="yiyi-120">位置导向（Python切片风格：不包括end）</span></li>
<li><span class="yiyi-st" id="yiyi-121">面向标签（非Python切片样式：包括​​end）</span></li>
<li><span class="yiyi-st" id="yiyi-122">一般（切片样式：取决于切片是否包含标签或位置）</span></li>
</ol>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [32]: </span><span class="n">df</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">&apos;bar&apos;</span><span class="p">:</span><span class="s1">&apos;kar&apos;</span><span class="p">]</span> <span class="c1">#Label</span>
<span class="gr">Out[32]: </span>
<span class="go">     AAA  BBB  CCC</span>
<span class="go">bar    5   20   50</span>
<span class="go">boo    6   30  -30</span>
<span class="go">kar    7   40  -50</span>

<span class="c">#Generic</span>
<span class="gp">In [33]: </span><span class="n">df</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span> <span class="c1">#Same as .iloc[0:3]</span>
<span class="gr">Out[33]: </span>
<span class="go">     AAA  BBB  CCC</span>
<span class="go">foo    4   10  100</span>
<span class="go">bar    5   20   50</span>
<span class="go">boo    6   30  -30</span>

<span class="gp">In [34]: </span><span class="n">df</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="s1">&apos;bar&apos;</span><span class="p">:</span><span class="s1">&apos;kar&apos;</span><span class="p">]</span> <span class="c1">#Same as .loc[&apos;bar&apos;:&apos;kar&apos;]</span>
<span class="gr">Out[34]: </span>
<span class="go">     AAA  BBB  CCC</span>
<span class="go">bar    5   20   50</span>
<span class="go">boo    6   30  -30</span>
<span class="go">kar    7   40  -50</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-123">当索引由具有非零开始或非单位增量的整数组成时，会出现模糊性。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [35]: </span><span class="n">df2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">data</span><span class="p">,</span><span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">]);</span> <span class="c1">#Note index starts at 1.</span>

<span class="gp">In [36]: </span><span class="n">df2</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span> <span class="c1">#Position-oriented</span>
<span class="gr">Out[36]: </span>
<span class="go">   AAA  BBB  CCC</span>
<span class="go">2    5   20   50</span>
<span class="go">3    6   30  -30</span>

<span class="gp">In [37]: </span><span class="n">df2</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span> <span class="c1">#Label-oriented</span>
<span class="gr">Out[37]: </span>
<span class="go">   AAA  BBB  CCC</span>
<span class="go">1    4   10  100</span>
<span class="go">2    5   20   50</span>
<span class="go">3    6   30  -30</span>

<span class="gp">In [38]: </span><span class="n">df2</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span> <span class="c1">#General, will mimic loc (label-oriented)</span>
<span class="gr">Out[38]: </span>
<span class="go">   AAA  BBB  CCC</span>
<span class="go">1    4   10  100</span>
<span class="go">2    5   20   50</span>
<span class="go">3    6   30  -30</span>

<span class="gp">In [39]: </span><span class="n">df2</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span> <span class="c1">#General, will mimic iloc (position-oriented), as loc[0:3] would raise a KeyError</span>
<span class="gr">Out[39]: </span>
<span class="go">   AAA  BBB  CCC</span>
<span class="go">1    4   10  100</span>
<span class="go">2    5   20   50</span>
<span class="go">3    6   30  -30</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-124"><a class="reference external" href="http://stackoverflow.com/questions/14986510/picking-out-elements-based-on-complement-of-indices-in-python-pandas">使用反向运算符（〜）来取掩码的补码</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [40]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span>
<span class="gp">   ....:</span>      <span class="p">{</span><span class="s1">&apos;AAA&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">],</span> <span class="s1">&apos;BBB&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span><span class="mi">20</span><span class="p">,</span><span class="mi">30</span><span class="p">,</span><span class="mi">40</span><span class="p">],</span> <span class="s1">&apos;CCC&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">100</span><span class="p">,</span><span class="mi">50</span><span class="p">,</span><span class="o">-</span><span class="mi">30</span><span class="p">,</span><span class="o">-</span><span class="mi">50</span><span class="p">]});</span> <span class="n">df</span>
<span class="gp">   ....:</span> 
<span class="gr">Out[40]: </span>
<span class="go">   AAA  BBB  CCC</span>
<span class="go">0    4   10  100</span>
<span class="go">1    5   20   50</span>
<span class="go">2    6   30  -30</span>
<span class="go">3    7   40  -50</span>

<span class="gp">In [41]: </span><span class="n">df</span><span class="p">[</span><span class="o">~</span><span class="p">((</span><span class="n">df</span><span class="o">.</span><span class="n">AAA</span> <span class="o">&lt;=</span> <span class="mi">6</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">isin</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">4</span><span class="p">])))]</span>
<span class="gr">Out[41]: </span>
<span class="go">   AAA  BBB  CCC</span>
<span class="go">1    5   20   50</span>
<span class="go">3    7   40  -50</span>
</pre></div>
</div>
</div>
<div class="section" id="panels">
<h3><span class="yiyi-st" id="yiyi-125">Panels</span></h3>
<p><span class="yiyi-st" id="yiyi-126"><a class="reference external" href="http://stackoverflow.com/questions/15364050/extending-a-pandas-panel-frame-along-the-minor-axis">通过移调，添加新尺寸并转换回原始尺寸来扩展面板框架</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [42]: </span><span class="n">rng</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;1/1/2013&apos;</span><span class="p">,</span><span class="n">periods</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span><span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">)</span>

<span class="gp">In [43]: </span><span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>

<span class="gp">In [44]: </span><span class="n">cols</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span><span class="s1">&apos;B&apos;</span><span class="p">,</span><span class="s1">&apos;C&apos;</span><span class="p">,</span><span class="s1">&apos;D&apos;</span><span class="p">]</span>

<span class="gp">In [45]: </span><span class="n">df1</span><span class="p">,</span> <span class="n">df2</span><span class="p">,</span> <span class="n">df3</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">rng</span><span class="p">,</span> <span class="n">cols</span><span class="p">),</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">rng</span><span class="p">,</span> <span class="n">cols</span><span class="p">),</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">rng</span><span class="p">,</span> <span class="n">cols</span><span class="p">)</span>

<span class="gp">In [46]: </span><span class="n">pf</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Panel</span><span class="p">({</span><span class="s1">&apos;df1&apos;</span><span class="p">:</span><span class="n">df1</span><span class="p">,</span><span class="s1">&apos;df2&apos;</span><span class="p">:</span><span class="n">df2</span><span class="p">,</span><span class="s1">&apos;df3&apos;</span><span class="p">:</span><span class="n">df3</span><span class="p">});</span><span class="n">pf</span>
<span class="gr">Out[46]: </span>
<span class="go">&lt;class &apos;pandas.core.panel.Panel&apos;&gt;</span>
<span class="go">Dimensions: 3 (items) x 100 (major_axis) x 4 (minor_axis)</span>
<span class="go">Items axis: df1 to df3</span>
<span class="go">Major_axis axis: 2013-01-01 00:00:00 to 2013-04-10 00:00:00</span>
<span class="go">Minor_axis axis: A to D</span>

<span class="c">#Assignment using Transpose  (pandas &lt; 0.15)</span>
<span class="gp">In [47]: </span><span class="n">pf</span> <span class="o">=</span> <span class="n">pf</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span>

<span class="gp">In [48]: </span><span class="n">pf</span><span class="p">[</span><span class="s1">&apos;E&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">rng</span><span class="p">,</span> <span class="n">cols</span><span class="p">)</span>

<span class="gp">In [49]: </span><span class="n">pf</span> <span class="o">=</span> <span class="n">pf</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span><span class="n">pf</span>
<span class="gr">Out[49]: </span>
<span class="go">&lt;class &apos;pandas.core.panel.Panel&apos;&gt;</span>
<span class="go">Dimensions: 3 (items) x 100 (major_axis) x 5 (minor_axis)</span>
<span class="go">Items axis: df1 to df3</span>
<span class="go">Major_axis axis: 2013-01-01 00:00:00 to 2013-04-10 00:00:00</span>
<span class="go">Minor_axis axis: A to E</span>

<span class="c">#Direct assignment (pandas &gt; 0.15)</span>
<span class="gp">In [50]: </span><span class="n">pf</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,:,</span><span class="s1">&apos;F&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">rng</span><span class="p">,</span> <span class="n">cols</span><span class="p">);</span><span class="n">pf</span>
<span class="gr">Out[50]: </span>
<span class="go">&lt;class &apos;pandas.core.panel.Panel&apos;&gt;</span>
<span class="go">Dimensions: 3 (items) x 100 (major_axis) x 6 (minor_axis)</span>
<span class="go">Items axis: df1 to df3</span>
<span class="go">Major_axis axis: 2013-01-01 00:00:00 to 2013-04-10 00:00:00</span>
<span class="go">Minor_axis axis: A to F</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-127"><a class="reference external" href="http://stackoverflow.com/questions/14650341/boolean-mask-in-pandas-panel">使用np.where屏蔽面板，然后使用新的屏蔽值</a>重建面板</span></p>
</div>
<div class="section" id="new-columns">
<h3><span class="yiyi-st" id="yiyi-128">New Columns</span></h3>
<p><span class="yiyi-st" id="yiyi-129"><a class="reference external" href="http://stackoverflow.com/questions/16575868/efficiently-creating-additional-columns-in-a-pandas-dataframe-using-map">使用applymap</a>有效且动态地创建新列</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [51]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span>
<span class="gp">   ....:</span>      <span class="p">{</span><span class="s1">&apos;AAA&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">],</span> <span class="s1">&apos;BBB&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">],</span> <span class="s1">&apos;CCC&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">1</span><span class="p">]});</span> <span class="n">df</span>
<span class="gp">   ....:</span> 
<span class="gr">Out[51]: </span>
<span class="go">   AAA  BBB  CCC</span>
<span class="go">0    1    1    2</span>
<span class="go">1    2    1    1</span>
<span class="go">2    1    2    3</span>
<span class="go">3    3    2    1</span>

<span class="gp">In [52]: </span><span class="n">source_cols</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">columns</span> <span class="c1"># or some subset would work too.</span>

<span class="gp">In [53]: </span><span class="n">new_cols</span> <span class="o">=</span> <span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;_cat&quot;</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">source_cols</span><span class="p">]</span>

<span class="gp">In [54]: </span><span class="n">categories</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span> <span class="p">:</span> <span class="s1">&apos;Alpha&apos;</span><span class="p">,</span> <span class="mi">2</span> <span class="p">:</span> <span class="s1">&apos;Beta&apos;</span><span class="p">,</span> <span class="mi">3</span> <span class="p">:</span> <span class="s1">&apos;Charlie&apos;</span> <span class="p">}</span>

<span class="gp">In [55]: </span><span class="n">df</span><span class="p">[</span><span class="n">new_cols</span><span class="p">]</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="n">source_cols</span><span class="p">]</span><span class="o">.</span><span class="n">applymap</span><span class="p">(</span><span class="n">categories</span><span class="o">.</span><span class="n">get</span><span class="p">);</span><span class="n">df</span>
<span class="gr">Out[55]: </span>
<span class="go">   AAA  BBB  CCC  AAA_cat BBB_cat  CCC_cat</span>
<span class="go">0    1    1    2    Alpha   Alpha     Beta</span>
<span class="go">1    2    1    1     Beta   Alpha    Alpha</span>
<span class="go">2    1    2    3    Alpha    Beta  Charlie</span>
<span class="go">3    3    2    1  Charlie    Beta    Alpha</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-130"><a class="reference external" href="http://stackoverflow.com/questions/23394476/keep-other-columns-when-using-min-with-groupby">在groupby</a>中使用min()时保留其他列</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [56]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span>
<span class="gp">   ....:</span>      <span class="p">{</span><span class="s1">&apos;AAA&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">],</span> <span class="s1">&apos;BBB&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">]});</span> <span class="n">df</span>
<span class="gp">   ....:</span> 
<span class="gr">Out[56]: </span>
<span class="go">   AAA  BBB</span>
<span class="go">0    1    2</span>
<span class="go">1    1    1</span>
<span class="go">2    1    3</span>
<span class="go">3    2    4</span>
<span class="go">4    2    5</span>
<span class="go">5    2    1</span>
<span class="go">6    3    2</span>
<span class="go">7    3    3</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-131">方法1：idxmin()获取最小的索引的完整数据原格式</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [57]: </span><span class="n">df</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">df</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="s2">&quot;AAA&quot;</span><span class="p">)[</span><span class="s2">&quot;BBB&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">idxmin</span><span class="p">()]</span>
<span class="gr">Out[57]: </span>
<span class="go">   AAA  BBB</span>
<span class="go">1    1    1</span>
<span class="go">5    2    1</span>
<span class="go">6    3    2</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-132">方法2：排序，然后取第一</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [58]: </span><span class="n">df</span><span class="o">.</span><span class="n">sort_values</span><span class="p">(</span><span class="n">by</span><span class="o">=</span><span class="s2">&quot;BBB&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="s2">&quot;AAA&quot;</span><span class="p">,</span> <span class="n">as_index</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span><span class="o">.</span><span class="n">first</span><span class="p">()</span>
<span class="gr">Out[58]: </span>
<span class="go">   AAA  BBB</span>
<span class="go">0    1    1</span>
<span class="go">1    2    1</span>
<span class="go">2    3    2</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-133">注意相同的结果，除了索引。</span></p>
</div>
</div>
<div class="section" id="multiindexing">
<span id="cookbook-multi-index"></span><h2><span class="yiyi-st" id="yiyi-134">MultiIndexing</span></h2>
<p><span class="yiyi-st" id="yiyi-135"><a class="reference internal" href="advanced.html#advanced-hierarchical"><span class="std std-ref">multindexing</span></a>。</span></p>
<p><span class="yiyi-st" id="yiyi-136"><a class="reference external" href="http://stackoverflow.com/questions/14916358/reshaping-dataframes-in-pandas-based-on-column-labels">从带标签的DataFrame创建多索引</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [59]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;row&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">],</span>
<span class="gp">   ....:</span>                    <span class="s1">&apos;One_X&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mf">1.1</span><span class="p">,</span><span class="mf">1.1</span><span class="p">,</span><span class="mf">1.1</span><span class="p">],</span>
<span class="gp">   ....:</span>                    <span class="s1">&apos;One_Y&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mf">1.2</span><span class="p">,</span><span class="mf">1.2</span><span class="p">,</span><span class="mf">1.2</span><span class="p">],</span>
<span class="gp">   ....:</span>                    <span class="s1">&apos;Two_X&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mf">1.11</span><span class="p">,</span><span class="mf">1.11</span><span class="p">,</span><span class="mf">1.11</span><span class="p">],</span>
<span class="gp">   ....:</span>                    <span class="s1">&apos;Two_Y&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mf">1.22</span><span class="p">,</span><span class="mf">1.22</span><span class="p">,</span><span class="mf">1.22</span><span class="p">]});</span> <span class="n">df</span>
<span class="gp">   ....:</span> 
<span class="gr">Out[59]: </span>
<span class="go">   One_X  One_Y  Two_X  Two_Y  row</span>
<span class="go">0    1.1    1.2   1.11   1.22    0</span>
<span class="go">1    1.1    1.2   1.11   1.22    1</span>
<span class="go">2    1.1    1.2   1.11   1.22    2</span>

<span class="c"># As Labelled Index</span>
<span class="gp">In [60]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&apos;row&apos;</span><span class="p">);</span><span class="n">df</span>
<span class="gr">Out[60]: </span>
<span class="go">     One_X  One_Y  Two_X  Two_Y</span>
<span class="go">row                            </span>
<span class="go">0      1.1    1.2   1.11   1.22</span>
<span class="go">1      1.1    1.2   1.11   1.22</span>
<span class="go">2      1.1    1.2   1.11   1.22</span>

<span class="c"># With Hierarchical Columns</span>
<span class="gp">In [61]: </span><span class="n">df</span><span class="o">.</span><span class="n">columns</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">MultiIndex</span><span class="o">.</span><span class="n">from_tuples</span><span class="p">([</span><span class="nb">tuple</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&apos;_&apos;</span><span class="p">))</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">df</span><span class="o">.</span><span class="n">columns</span><span class="p">]);</span><span class="n">df</span>
<span class="gr">Out[61]: </span>
<span class="go">     One        Two      </span>
<span class="go">       X    Y     X     Y</span>
<span class="go">row                      </span>
<span class="go">0    1.1  1.2  1.11  1.22</span>
<span class="go">1    1.1  1.2  1.11  1.22</span>
<span class="go">2    1.1  1.2  1.11  1.22</span>

<span class="c"># Now stack &amp; Reset</span>
<span class="gp">In [62]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">stack</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">reset_index</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span><span class="n">df</span>
<span class="gr">Out[62]: </span>
<span class="go">    level_1     X     Y</span>
<span class="go">row                    </span>
<span class="go">0       One  1.10  1.20</span>
<span class="go">0       Two  1.11  1.22</span>
<span class="go">1       One  1.10  1.20</span>
<span class="go">1       Two  1.11  1.22</span>
<span class="go">2       One  1.10  1.20</span>
<span class="go">2       Two  1.11  1.22</span>

<span class="c"># And fix the labels (Notice the label &apos;level_1&apos; got added automatically)</span>
<span class="gp">In [63]: </span><span class="n">df</span><span class="o">.</span><span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&apos;Sample&apos;</span><span class="p">,</span><span class="s1">&apos;All_X&apos;</span><span class="p">,</span><span class="s1">&apos;All_Y&apos;</span><span class="p">];</span><span class="n">df</span>
<span class="gr">Out[63]: </span>
<span class="go">    Sample  All_X  All_Y</span>
<span class="go">row                     </span>
<span class="go">0      One   1.10   1.20</span>
<span class="go">0      Two   1.11   1.22</span>
<span class="go">1      One   1.10   1.20</span>
<span class="go">1      Two   1.11   1.22</span>
<span class="go">2      One   1.10   1.20</span>
<span class="go">2      Two   1.11   1.22</span>
</pre></div>
</div>
<div class="section" id="arithmetic">
<h3><span class="yiyi-st" id="yiyi-137">Arithmetic</span></h3>
<p><span class="yiyi-st" id="yiyi-138"><a class="reference external" href="http://stackoverflow.com/questions/19501510/divide-entire-pandas-multiindex-dataframe-by-dataframe-variable/19502176#19502176">使用需要广播的多索引进行算术</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [64]: </span><span class="n">cols</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">MultiIndex</span><span class="o">.</span><span class="n">from_tuples</span><span class="p">([</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span><span class="s1">&apos;B&apos;</span><span class="p">,</span><span class="s1">&apos;C&apos;</span><span class="p">]</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&apos;O&apos;</span><span class="p">,</span><span class="s1">&apos;I&apos;</span><span class="p">]])</span>

<span class="gp">In [65]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">6</span><span class="p">),</span><span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;n&apos;</span><span class="p">,</span><span class="s1">&apos;m&apos;</span><span class="p">],</span><span class="n">columns</span><span class="o">=</span><span class="n">cols</span><span class="p">);</span> <span class="n">df</span>
<span class="gr">Out[65]: </span>
<span class="go">          A                   B                   C          </span>
<span class="go">          O         I         O         I         O         I</span>
<span class="go">n  1.920906 -0.388231 -2.314394  0.665508  0.402562  0.399555</span>
<span class="go">m -1.765956  0.850423  0.388054  0.992312  0.744086 -0.739776</span>

<span class="gp">In [66]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">div</span><span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;C&apos;</span><span class="p">],</span><span class="n">level</span><span class="o">=</span><span class="mi">1</span><span class="p">);</span> <span class="n">df</span>
<span class="gr">Out[66]: </span>
<span class="go">          A                   B              C     </span>
<span class="go">          O         I         O         I    O    I</span>
<span class="go">n  4.771702 -0.971660 -5.749162  1.665625  1.0  1.0</span>
<span class="go">m -2.373321 -1.149568  0.521518 -1.341367  1.0  1.0</span>
</pre></div>
</div>
</div>
<div class="section" id="slicing">
<h3><span class="yiyi-st" id="yiyi-139">Slicing</span></h3>
<p><span class="yiyi-st" id="yiyi-140"><a class="reference external" href="http://stackoverflow.com/questions/12590131/how-to-slice-multindex-columns-in-pandas-dataframes">使用xs</a>切片多索引</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [67]: </span><span class="n">coords</span> <span class="o">=</span> <span class="p">[(</span><span class="s1">&apos;AA&apos;</span><span class="p">,</span><span class="s1">&apos;one&apos;</span><span class="p">),(</span><span class="s1">&apos;AA&apos;</span><span class="p">,</span><span class="s1">&apos;six&apos;</span><span class="p">),(</span><span class="s1">&apos;BB&apos;</span><span class="p">,</span><span class="s1">&apos;one&apos;</span><span class="p">),(</span><span class="s1">&apos;BB&apos;</span><span class="p">,</span><span class="s1">&apos;two&apos;</span><span class="p">),(</span><span class="s1">&apos;BB&apos;</span><span class="p">,</span><span class="s1">&apos;six&apos;</span><span class="p">)]</span>

<span class="gp">In [68]: </span><span class="n">index</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">MultiIndex</span><span class="o">.</span><span class="n">from_tuples</span><span class="p">(</span><span class="n">coords</span><span class="p">)</span>

<span class="gp">In [69]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">([</span><span class="mi">11</span><span class="p">,</span><span class="mi">22</span><span class="p">,</span><span class="mi">33</span><span class="p">,</span><span class="mi">44</span><span class="p">,</span><span class="mi">55</span><span class="p">],</span><span class="n">index</span><span class="p">,[</span><span class="s1">&apos;MyData&apos;</span><span class="p">]);</span> <span class="n">df</span>
<span class="gr">Out[69]: </span>
<span class="go">        MyData</span>
<span class="go">AA one      11</span>
<span class="go">   six      22</span>
<span class="go">BB one      33</span>
<span class="go">   two      44</span>
<span class="go">   six      55</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-141">要取第1级和第1轴的横截面索引：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [70]: </span><span class="n">df</span><span class="o">.</span><span class="n">xs</span><span class="p">(</span><span class="s1">&apos;BB&apos;</span><span class="p">,</span><span class="n">level</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>  <span class="c1">#Note : level and axis are optional, and default to zero</span>
<span class="gr">Out[70]: </span>
<span class="go">     MyData</span>
<span class="go">one      33</span>
<span class="go">two      44</span>
<span class="go">six      55</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-142">...现在是第1轴的第2级。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [71]: </span><span class="n">df</span><span class="o">.</span><span class="n">xs</span><span class="p">(</span><span class="s1">&apos;six&apos;</span><span class="p">,</span><span class="n">level</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[71]: </span>
<span class="go">    MyData</span>
<span class="go">AA      22</span>
<span class="go">BB      55</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-143"><a class="reference external" href="http://stackoverflow.com/questions/14964493/multiindex-based-indexing-in-pandas">使用xs，方法＃2</a>来切割多索引</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [72]: </span><span class="n">index</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">([</span><span class="s1">&apos;Ada&apos;</span><span class="p">,</span><span class="s1">&apos;Quinn&apos;</span><span class="p">,</span><span class="s1">&apos;Violet&apos;</span><span class="p">],[</span><span class="s1">&apos;Comp&apos;</span><span class="p">,</span><span class="s1">&apos;Math&apos;</span><span class="p">,</span><span class="s1">&apos;Sci&apos;</span><span class="p">]))</span>

<span class="gp">In [73]: </span><span class="n">headr</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">([</span><span class="s1">&apos;Exams&apos;</span><span class="p">,</span><span class="s1">&apos;Labs&apos;</span><span class="p">],[</span><span class="s1">&apos;I&apos;</span><span class="p">,</span><span class="s1">&apos;II&apos;</span><span class="p">]))</span>

<span class="gp">In [74]: </span><span class="n">indx</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">MultiIndex</span><span class="o">.</span><span class="n">from_tuples</span><span class="p">(</span><span class="n">index</span><span class="p">,</span><span class="n">names</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;Student&apos;</span><span class="p">,</span><span class="s1">&apos;Course&apos;</span><span class="p">])</span>

<span class="gp">In [75]: </span><span class="n">cols</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">MultiIndex</span><span class="o">.</span><span class="n">from_tuples</span><span class="p">(</span><span class="n">headr</span><span class="p">)</span> <span class="c1">#Notice these are un-named</span>

<span class="gp">In [76]: </span><span class="n">data</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">70</span><span class="o">+</span><span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="o">+</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="p">)</span><span class="o">%</span><span class="mi">3</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">)]</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">9</span><span class="p">)]</span>

<span class="gp">In [77]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="p">,</span><span class="n">indx</span><span class="p">,</span><span class="n">cols</span><span class="p">);</span> <span class="n">df</span>
<span class="gr">Out[77]: </span>
<span class="go">               Exams     Labs    </span>
<span class="go">                   I  II    I  II</span>
<span class="go">Student Course                   </span>
<span class="go">Ada     Comp      70  71   72  73</span>
<span class="go">        Math      71  73   75  74</span>
<span class="go">        Sci       72  75   75  75</span>
<span class="go">Quinn   Comp      73  74   75  76</span>
<span class="go">        Math      74  76   78  77</span>
<span class="go">        Sci       75  78   78  78</span>
<span class="go">Violet  Comp      76  77   78  79</span>
<span class="go">        Math      77  79   81  80</span>
<span class="go">        Sci       78  81   81  81</span>

<span class="gp">In [78]: </span><span class="n">All</span> <span class="o">=</span> <span class="nb">slice</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span>

<span class="gp">In [79]: </span><span class="n">df</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">&apos;Violet&apos;</span><span class="p">]</span>
<span class="gr">Out[79]: </span>
<span class="go">       Exams     Labs    </span>
<span class="go">           I  II    I  II</span>
<span class="go">Course                   </span>
<span class="go">Comp      76  77   78  79</span>
<span class="go">Math      77  79   81  80</span>
<span class="go">Sci       78  81   81  81</span>

<span class="gp">In [80]: </span><span class="n">df</span><span class="o">.</span><span class="n">loc</span><span class="p">[(</span><span class="n">All</span><span class="p">,</span><span class="s1">&apos;Math&apos;</span><span class="p">),</span><span class="n">All</span><span class="p">]</span>
<span class="gr">Out[80]: </span>
<span class="go">               Exams     Labs    </span>
<span class="go">                   I  II    I  II</span>
<span class="go">Student Course                   </span>
<span class="go">Ada     Math      71  73   75  74</span>
<span class="go">Quinn   Math      74  76   78  77</span>
<span class="go">Violet  Math      77  79   81  80</span>

<span class="gp">In [81]: </span><span class="n">df</span><span class="o">.</span><span class="n">loc</span><span class="p">[(</span><span class="nb">slice</span><span class="p">(</span><span class="s1">&apos;Ada&apos;</span><span class="p">,</span><span class="s1">&apos;Quinn&apos;</span><span class="p">),</span><span class="s1">&apos;Math&apos;</span><span class="p">),</span><span class="n">All</span><span class="p">]</span>
<span class="gr">Out[81]: </span>
<span class="go">               Exams     Labs    </span>
<span class="go">                   I  II    I  II</span>
<span class="go">Student Course                   </span>
<span class="go">Ada     Math      71  73   75  74</span>
<span class="go">Quinn   Math      74  76   78  77</span>

<span class="gp">In [82]: </span><span class="n">df</span><span class="o">.</span><span class="n">loc</span><span class="p">[(</span><span class="n">All</span><span class="p">,</span><span class="s1">&apos;Math&apos;</span><span class="p">),(</span><span class="s1">&apos;Exams&apos;</span><span class="p">)]</span>
<span class="gr">Out[82]: </span>
<span class="go">                 I  II</span>
<span class="go">Student Course        </span>
<span class="go">Ada     Math    71  73</span>
<span class="go">Quinn   Math    74  76</span>
<span class="go">Violet  Math    77  79</span>

<span class="gp">In [83]: </span><span class="n">df</span><span class="o">.</span><span class="n">loc</span><span class="p">[(</span><span class="n">All</span><span class="p">,</span><span class="s1">&apos;Math&apos;</span><span class="p">),(</span><span class="n">All</span><span class="p">,</span><span class="s1">&apos;II&apos;</span><span class="p">)]</span>
<span class="gr">Out[83]: </span>
<span class="go">               Exams Labs</span>
<span class="go">                  II   II</span>
<span class="go">Student Course           </span>
<span class="go">Ada     Math      73   74</span>
<span class="go">Quinn   Math      76   77</span>
<span class="go">Violet  Math      79   80</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-144"><a class="reference external" href="http://stackoverflow.com/questions/19319432/pandas-selecting-a-lower-level-in-a-dataframe-to-do-a-ffill">使用xs</a>设置多索引的部分</span></p>
</div>
<div class="section" id="sorting">
<h3><span class="yiyi-st" id="yiyi-145">Sorting</span></h3>
<p><span class="yiyi-st" id="yiyi-146"><a class="reference external" href="http://stackoverflow.com/questions/14733871/mutli-index-sorting-in-pandas">按特定列或使用多索引</a>的列的有序列表排序</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [84]: </span><span class="n">df</span><span class="o">.</span><span class="n">sort_values</span><span class="p">(</span><span class="n">by</span><span class="o">=</span><span class="p">(</span><span class="s1">&apos;Labs&apos;</span><span class="p">,</span> <span class="s1">&apos;II&apos;</span><span class="p">),</span> <span class="n">ascending</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gr">Out[84]: </span>
<span class="go">               Exams     Labs    </span>
<span class="go">                   I  II    I  II</span>
<span class="go">Student Course                   </span>
<span class="go">Violet  Sci       78  81   81  81</span>
<span class="go">        Math      77  79   81  80</span>
<span class="go">        Comp      76  77   78  79</span>
<span class="go">Quinn   Sci       75  78   78  78</span>
<span class="go">        Math      74  76   78  77</span>
<span class="go">        Comp      73  74   75  76</span>
<span class="go">Ada     Sci       72  75   75  75</span>
<span class="go">        Math      71  73   75  74</span>
<span class="go">        Comp      70  71   72  73</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-147"><a class="reference external" href="https://github.com/pandas-dev/pandas/issues/2995">部分选择，需要排序；</a></span></p>
</div>
<div class="section" id="levels">
<h3><span class="yiyi-st" id="yiyi-148">Levels</span></h3>
<p><span class="yiyi-st" id="yiyi-149"><a class="reference external" href="http://stackoverflow.com/questions/14744068/prepend-a-level-to-a-pandas-multiindex">将关卡预置到多索引</a></span></p>
<p><span class="yiyi-st" id="yiyi-150"><a class="reference external" href="http://stackoverflow.com/questions/14507794/python-pandas-how-to-flatten-a-hierarchical-index-in-columns">展平分层列</a></span></p>
</div>
<div class="section" id="panelnd">
<h3><span class="yiyi-st" id="yiyi-151">panelnd</span></h3>
<p><span class="yiyi-st" id="yiyi-152"><a class="reference internal" href="dsintro.html#dsintro-panelnd"><span class="std std-ref">panelnd</span></a>文档。</span></p>
<p><span class="yiyi-st" id="yiyi-153"><a class="reference external" href="http://stackoverflow.com/questions/18748598/why-my-panelnd-factory-throwing-a-keyerror">构建5D面板</a></span></p>
</div>
</div>
<div class="section" id="missing-data">
<span id="cookbook-missing-data"></span><h2><span class="yiyi-st" id="yiyi-154">Missing Data</span></h2>
<p><span class="yiyi-st" id="yiyi-155"><a class="reference internal" href="missing_data.html#missing-data"><span class="std std-ref">missing data</span></a>文档。</span></p>
<p><span class="yiyi-st" id="yiyi-156">填充反向时间序列</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [85]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span><span class="mi">1</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;2013-08-01&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;B&apos;</span><span class="p">),</span> <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;A&apos;</span><span class="p">))</span>

<span class="gp">In [86]: </span><span class="n">df</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="s1">&apos;A&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>

<span class="gp">In [87]: </span><span class="n">df</span>
<span class="gr">Out[87]: </span>
<span class="go">                   A</span>
<span class="go">2013-08-01 -1.054874</span>
<span class="go">2013-08-02 -0.179642</span>
<span class="go">2013-08-05  0.639589</span>
<span class="go">2013-08-06       NaN</span>
<span class="go">2013-08-07  1.906684</span>
<span class="go">2013-08-08  0.104050</span>

<span class="gp">In [88]: </span><span class="n">df</span><span class="o">.</span><span class="n">reindex</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="p">[::</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span><span class="o">.</span><span class="n">ffill</span><span class="p">()</span>
<span class="gr">Out[88]: </span>
<span class="go">                   A</span>
<span class="go">2013-08-08  0.104050</span>
<span class="go">2013-08-07  1.906684</span>
<span class="go">2013-08-06  1.906684</span>
<span class="go">2013-08-05  0.639589</span>
<span class="go">2013-08-02 -0.179642</span>
<span class="go">2013-08-01 -1.054874</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-157"><a class="reference external" href="http://stackoverflow.com/questions/18196811/cumsum-reset-at-nan">在NaN值</a>的累计复位</span></p>
<div class="section" id="replace">
<h3><span class="yiyi-st" id="yiyi-158">Replace</span></h3>
<p><span class="yiyi-st" id="yiyi-159"><a class="reference external" href="http://stackoverflow.com/questions/16818871/extracting-value-and-creating-new-column-out-of-it">使用替换为backrefs</a></span></p>
</div>
</div>
<div class="section" id="grouping">
<span id="cookbook-grouping"></span><h2><span class="yiyi-st" id="yiyi-160">Grouping</span></h2>
<p><span class="yiyi-st" id="yiyi-161"><a class="reference internal" href="groupby.html#groupby"><span class="std std-ref">grouping</span></a>文档。</span></p>
<p><span class="yiyi-st" id="yiyi-162"><a class="reference external" href="http://stackoverflow.com/questions/15322632/python-pandas-df-groupy-agg-column-reference-in-agg">应用</a>的基本分组</span></p>
<p><span class="yiyi-st" id="yiyi-163">与agg不同，apply的callable是通过一个子DataFrame传递给你访问的所有的列</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [89]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;animal&apos;</span><span class="p">:</span> <span class="s1">&apos;cat dog cat fish dog cat cat&apos;</span><span class="o">.</span><span class="n">split</span><span class="p">(),</span>
<span class="gp">   ....:</span>                    <span class="s1">&apos;size&apos;</span><span class="p">:</span> <span class="nb">list</span><span class="p">(</span><span class="s1">&apos;SSMMMLL&apos;</span><span class="p">),</span>
<span class="gp">   ....:</span>                    <span class="s1">&apos;weight&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">8</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">12</span><span class="p">],</span>
<span class="gp">   ....:</span>                    <span class="s1">&apos;adult&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="bp">False</span><span class="p">]</span> <span class="o">*</span> <span class="mi">5</span> <span class="o">+</span> <span class="p">[</span><span class="bp">True</span><span class="p">]</span> <span class="o">*</span> <span class="mi">2</span><span class="p">});</span> <span class="n">df</span>
<span class="gp">   ....:</span> 
<span class="gr">Out[89]: </span>
<span class="go">   adult animal size  weight</span>
<span class="go">0  False    cat    S       8</span>
<span class="go">1  False    dog    S      10</span>
<span class="go">2  False    cat    M      11</span>
<span class="go">3  False   fish    M       1</span>
<span class="go">4  False    dog    M      20</span>
<span class="go">5   True    cat    L      12</span>
<span class="go">6   True    cat    L      12</span>

<span class="c">#List the size of the animals with the highest weight.</span>
<span class="gp">In [90]: </span><span class="n">df</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="s1">&apos;animal&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">subf</span><span class="p">:</span> <span class="n">subf</span><span class="p">[</span><span class="s1">&apos;size&apos;</span><span class="p">][</span><span class="n">subf</span><span class="p">[</span><span class="s1">&apos;weight&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">idxmax</span><span class="p">()])</span>
<span class="gr">Out[90]: </span>
<span class="go">animal</span>
<span class="go">cat     L</span>
<span class="go">dog     M</span>
<span class="go">fish    M</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-164"><a class="reference external" href="http://stackoverflow.com/questions/14734533/how-to-access-pandas-groupby-dataframe-by-key">使用get_group</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [91]: </span><span class="n">gb</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">groupby</span><span class="p">([</span><span class="s1">&apos;animal&apos;</span><span class="p">])</span>

<span class="gp">In [92]: </span><span class="n">gb</span><span class="o">.</span><span class="n">get_group</span><span class="p">(</span><span class="s1">&apos;cat&apos;</span><span class="p">)</span>
<span class="gr">Out[92]: </span>
<span class="go">   adult animal size  weight</span>
<span class="go">0  False    cat    S       8</span>
<span class="go">2  False    cat    M      11</span>
<span class="go">5   True    cat    L      12</span>
<span class="go">6   True    cat    L      12</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-165"><a class="reference external" href="http://stackoverflow.com/questions/15262134/apply-different-functions-to-different-items-in-group-object-python-pandas">套用至群组中的其他项目</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [93]: </span><span class="k">def</span> <span class="nf">GrowUp</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="gp">   ....:</span>    <span class="n">avg_weight</span> <span class="o">=</span>  <span class="nb">sum</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="s1">&apos;size&apos;</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&apos;S&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">weight</span> <span class="o">*</span> <span class="mf">1.5</span><span class="p">)</span>
<span class="gp">   ....:</span>    <span class="n">avg_weight</span> <span class="o">+=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="s1">&apos;size&apos;</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&apos;M&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">weight</span> <span class="o">*</span> <span class="mf">1.25</span><span class="p">)</span>
<span class="gp">   ....:</span>    <span class="n">avg_weight</span> <span class="o">+=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="s1">&apos;size&apos;</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&apos;L&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">weight</span><span class="p">)</span>
<span class="gp">   ....:</span>    <span class="n">avg_weight</span> <span class="o">/=</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">   ....:</span>    <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s1">&apos;L&apos;</span><span class="p">,</span><span class="n">avg_weight</span><span class="p">,</span><span class="bp">True</span><span class="p">],</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;size&apos;</span><span class="p">,</span> <span class="s1">&apos;weight&apos;</span><span class="p">,</span> <span class="s1">&apos;adult&apos;</span><span class="p">])</span>
<span class="gp">   ....:</span> 

<span class="gp">In [94]: </span><span class="n">expected_df</span> <span class="o">=</span> <span class="n">gb</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">GrowUp</span><span class="p">)</span>

<span class="gp">In [95]: </span><span class="n">expected_df</span>
<span class="gr">Out[95]: </span>
<span class="go">       size   weight adult</span>
<span class="go">animal                    </span>
<span class="go">cat       L  12.4375  True</span>
<span class="go">dog       L  20.0000  True</span>
<span class="go">fish      L   1.2500  True</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-166"><a class="reference external" href="http://stackoverflow.com/questions/14542145/reductions-down-a-column-in-pandas">扩展应用</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [96]: </span><span class="n">S</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="n">i</span> <span class="o">/</span> <span class="mf">100.0</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">11</span><span class="p">)])</span>

<span class="gp">In [97]: </span><span class="k">def</span> <span class="nf">CumRet</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">):</span>
<span class="gp">   ....:</span>    <span class="k">return</span> <span class="n">x</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="n">y</span><span class="p">)</span>
<span class="gp">   ....:</span> 

<span class="gp">In [98]: </span><span class="k">def</span> <span class="nf">Red</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="gp">   ....:</span>    <span class="k">return</span> <span class="n">functools</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="n">CumRet</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="mf">1.0</span><span class="p">)</span>
<span class="gp">   ....:</span> 

<span class="gp">In [99]: </span><span class="n">S</span><span class="o">.</span><span class="n">expanding</span><span class="p">()</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">Red</span><span class="p">)</span>
<span class="gr">Out[99]: </span>
<span class="go">0    1.010000</span>
<span class="go">1    1.030200</span>
<span class="go">2    1.061106</span>
<span class="go">3    1.103550</span>
<span class="go">4    1.158728</span>
<span class="go">5    1.228251</span>
<span class="go">6    1.314229</span>
<span class="go">7    1.419367</span>
<span class="go">8    1.547110</span>
<span class="go">9    1.701821</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-167"><a class="reference external" href="http://stackoverflow.com/questions/14760757/replacing-values-with-groupby-means">用组的其余部分的平均值替换某些值</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [100]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;A&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="s1">&apos;B&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]})</span>

<span class="gp">In [101]: </span><span class="n">gb</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="s1">&apos;A&apos;</span><span class="p">)</span>

<span class="gp">In [102]: </span><span class="k">def</span> <span class="nf">replace</span><span class="p">(</span><span class="n">g</span><span class="p">):</span>
<span class="gp">   .....:</span>    <span class="n">mask</span> <span class="o">=</span> <span class="n">g</span> <span class="o">&lt;</span> <span class="mi">0</span>
<span class="gp">   .....:</span>    <span class="n">g</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">mask</span><span class="p">]</span> <span class="o">=</span> <span class="n">g</span><span class="p">[</span><span class="o">~</span><span class="n">mask</span><span class="p">]</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="gp">   .....:</span>    <span class="k">return</span> <span class="n">g</span>
<span class="gp">   .....:</span> 

<span class="gp">In [103]: </span><span class="n">gb</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">replace</span><span class="p">)</span>
<span class="gr">Out[103]: </span>
<span class="go">     B</span>
<span class="go">0  1.0</span>
<span class="go">1  1.0</span>
<span class="go">2  1.0</span>
<span class="go">3  2.0</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-168"><a class="reference external" href="http://stackoverflow.com/questions/14941366/pandas-sort-by-group-aggregate-and-column">按聚合数据对组排序</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [104]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;code&apos;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&apos;foo&apos;</span><span class="p">,</span> <span class="s1">&apos;bar&apos;</span><span class="p">,</span> <span class="s1">&apos;baz&apos;</span><span class="p">]</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;data&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mf">0.16</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.21</span><span class="p">,</span> <span class="mf">0.33</span><span class="p">,</span> <span class="mf">0.45</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.59</span><span class="p">,</span> <span class="mf">0.62</span><span class="p">],</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;flag&apos;</span><span class="p">:</span> <span class="p">[</span><span class="bp">False</span><span class="p">,</span> <span class="bp">True</span><span class="p">]</span> <span class="o">*</span> <span class="mi">3</span><span class="p">})</span>
<span class="gp">   .....:</span> 

<span class="gp">In [105]: </span><span class="n">code_groups</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="s1">&apos;code&apos;</span><span class="p">)</span>

<span class="gp">In [106]: </span><span class="n">agg_n_sort_order</span> <span class="o">=</span> <span class="n">code_groups</span><span class="p">[[</span><span class="s1">&apos;data&apos;</span><span class="p">]]</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="nb">sum</span><span class="p">)</span><span class="o">.</span><span class="n">sort_values</span><span class="p">(</span><span class="n">by</span><span class="o">=</span><span class="s1">&apos;data&apos;</span><span class="p">)</span>

<span class="gp">In [107]: </span><span class="n">sorted_df</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="n">agg_n_sort_order</span><span class="o">.</span><span class="n">index</span><span class="p">]</span>

<span class="gp">In [108]: </span><span class="n">sorted_df</span>
<span class="gr">Out[108]: </span>
<span class="go">  code  data   flag</span>
<span class="go">1  bar -0.21   True</span>
<span class="go">4  bar -0.59  False</span>
<span class="go">0  foo  0.16  False</span>
<span class="go">3  foo  0.45   True</span>
<span class="go">2  baz  0.33  False</span>
<span class="go">5  baz  0.62   True</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-169"><a class="reference external" href="http://stackoverflow.com/questions/14897100/create-multiple-columns-in-pandas-aggregation-function">创建多个聚合列</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [109]: </span><span class="n">rng</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="s2">&quot;2014-10-07&quot;</span><span class="p">,</span><span class="n">periods</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span><span class="n">freq</span><span class="o">=</span><span class="s1">&apos;2min&apos;</span><span class="p">)</span>

<span class="gp">In [110]: </span><span class="n">ts</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">data</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)),</span> <span class="n">index</span> <span class="o">=</span> <span class="n">rng</span><span class="p">)</span>

<span class="gp">In [111]: </span><span class="k">def</span> <span class="nf">MyCust</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="gp">   .....:</span>    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">:</span>
<span class="gp">   .....:</span>       <span class="k">return</span> <span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="mf">1.234</span>
<span class="gp">   .....:</span>    <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">NaT</span>
<span class="gp">   .....:</span> 

<span class="gp">In [112]: </span><span class="n">mhc</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&apos;Mean&apos;</span> <span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">,</span> <span class="s1">&apos;Max&apos;</span> <span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">,</span> <span class="s1">&apos;Custom&apos;</span> <span class="p">:</span> <span class="n">MyCust</span><span class="p">}</span>

<span class="gp">In [113]: </span><span class="n">ts</span><span class="o">.</span><span class="n">resample</span><span class="p">(</span><span class="s2">&quot;5min&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">mhc</span><span class="p">)</span>
<span class="gr">Out[113]: </span>
<span class="go">                     Max Custom  Mean</span>
<span class="go">2014-10-07 00:00:00    2  1.234   1.0</span>
<span class="go">2014-10-07 00:05:00    4    NaT   3.5</span>
<span class="go">2014-10-07 00:10:00    7  7.404   6.0</span>
<span class="go">2014-10-07 00:15:00    9    NaT   8.5</span>

<span class="gp">In [114]: </span><span class="n">ts</span>
<span class="gr">Out[114]: </span>
<span class="go">2014-10-07 00:00:00    0</span>
<span class="go">2014-10-07 00:02:00    1</span>
<span class="go">2014-10-07 00:04:00    2</span>
<span class="go">2014-10-07 00:06:00    3</span>
<span class="go">2014-10-07 00:08:00    4</span>
<span class="go">2014-10-07 00:10:00    5</span>
<span class="go">2014-10-07 00:12:00    6</span>
<span class="go">2014-10-07 00:14:00    7</span>
<span class="go">2014-10-07 00:16:00    8</span>
<span class="go">2014-10-07 00:18:00    9</span>
<span class="go">Freq: 2T, dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-170"><a class="reference external" href="http://stackoverflow.com/questions/17709270/i-want-to-create-a-column-of-value-counts-in-my-pandas-dataframe">创建值计数列，并重新分配回DataFrame</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [115]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;Color&apos;</span><span class="p">:</span> <span class="s1">&apos;Red Red Red Blue&apos;</span><span class="o">.</span><span class="n">split</span><span class="p">(),</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;Value&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">100</span><span class="p">,</span> <span class="mi">150</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="mi">50</span><span class="p">]});</span> <span class="n">df</span>
<span class="gp">   .....:</span> 
<span class="gr">Out[115]: </span>
<span class="go">  Color  Value</span>
<span class="go">0   Red    100</span>
<span class="go">1   Red    150</span>
<span class="go">2   Red     50</span>
<span class="go">3  Blue     50</span>

<span class="gp">In [116]: </span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;Counts&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">groupby</span><span class="p">([</span><span class="s1">&apos;Color&apos;</span><span class="p">])</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="nb">len</span><span class="p">)</span>

<span class="gp">In [117]: </span><span class="n">df</span>
<span class="gr">Out[117]: </span>
<span class="go">  Color  Value  Counts</span>
<span class="go">0   Red    100       3</span>
<span class="go">1   Red    150       3</span>
<span class="go">2   Red     50       3</span>
<span class="go">3  Blue     50       1</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-171"><a class="reference external" href="http://stackoverflow.com/q/23198053/190597">根据索引</a>对列中的值进行移位</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [118]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span>
<span class="gp">   .....:</span>    <span class="p">{</span><span class="s1">u&apos;line_race&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">8</span><span class="p">],</span>
<span class="gp">   .....:</span>     <span class="s1">u&apos;beyer&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">99</span><span class="p">,</span> <span class="mi">102</span><span class="p">,</span> <span class="mi">103</span><span class="p">,</span> <span class="mi">103</span><span class="p">,</span> <span class="mi">88</span><span class="p">,</span> <span class="mi">100</span><span class="p">]},</span>
<span class="gp">   .....:</span>     <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">u&apos;Last Gunfighter&apos;</span><span class="p">,</span> <span class="s1">u&apos;Last Gunfighter&apos;</span><span class="p">,</span> <span class="s1">u&apos;Last Gunfighter&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>            <span class="s1">u&apos;Paynter&apos;</span><span class="p">,</span> <span class="s1">u&apos;Paynter&apos;</span><span class="p">,</span> <span class="s1">u&apos;Paynter&apos;</span><span class="p">]);</span> <span class="n">df</span>
<span class="gp">   .....:</span> 
<span class="gr">Out[118]: </span>
<span class="go">                 beyer  line_race</span>
<span class="go">Last Gunfighter     99         10</span>
<span class="go">Last Gunfighter    102         10</span>
<span class="go">Last Gunfighter    103          8</span>
<span class="go">Paynter            103         10</span>
<span class="go">Paynter             88         10</span>
<span class="go">Paynter            100          8</span>

<span class="gp">In [119]: </span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;beyer_shifted&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="n">level</span><span class="o">=</span><span class="mi">0</span><span class="p">)[</span><span class="s1">&apos;beyer&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">shift</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

<span class="gp">In [120]: </span><span class="n">df</span>
<span class="gr">Out[120]: </span>
<span class="go">                 beyer  line_race  beyer_shifted</span>
<span class="go">Last Gunfighter     99         10            NaN</span>
<span class="go">Last Gunfighter    102         10           99.0</span>
<span class="go">Last Gunfighter    103          8          102.0</span>
<span class="go">Paynter            103         10            NaN</span>
<span class="go">Paynter             88         10          103.0</span>
<span class="go">Paynter            100          8           88.0</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-172"><a class="reference external" href="http://stackoverflow.com/q/26701849/190597">从每个组</a>中选择具有最大值的行</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [121]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;host&apos;</span><span class="p">:[</span><span class="s1">&apos;other&apos;</span><span class="p">,</span><span class="s1">&apos;other&apos;</span><span class="p">,</span><span class="s1">&apos;that&apos;</span><span class="p">,</span><span class="s1">&apos;this&apos;</span><span class="p">,</span><span class="s1">&apos;this&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;service&apos;</span><span class="p">:[</span><span class="s1">&apos;mail&apos;</span><span class="p">,</span><span class="s1">&apos;web&apos;</span><span class="p">,</span><span class="s1">&apos;mail&apos;</span><span class="p">,</span><span class="s1">&apos;mail&apos;</span><span class="p">,</span><span class="s1">&apos;web&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;no&apos;</span><span class="p">:[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">]})</span><span class="o">.</span><span class="n">set_index</span><span class="p">([</span><span class="s1">&apos;host&apos;</span><span class="p">,</span> <span class="s1">&apos;service&apos;</span><span class="p">])</span>
<span class="gp">   .....:</span> 

<span class="gp">In [122]: </span><span class="n">mask</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="n">level</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">agg</span><span class="p">(</span><span class="s1">&apos;idxmax&apos;</span><span class="p">)</span>

<span class="gp">In [123]: </span><span class="n">df_count</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">mask</span><span class="p">[</span><span class="s1">&apos;no&apos;</span><span class="p">]]</span><span class="o">.</span><span class="n">reset_index</span><span class="p">()</span>

<span class="gp">In [124]: </span><span class="n">df_count</span>
<span class="gr">Out[124]: </span>
<span class="go">    host service  no</span>
<span class="go">0  other     web   2</span>
<span class="go">1   that    mail   1</span>
<span class="go">2   this    mail   2</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-173"><a class="reference external" href="http://stackoverflow.com/q/29142487/846892">像Python的itertools.groupby</a>分组</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [125]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">])</span>

<span class="gp">In [126]: </span><span class="n">df</span><span class="o">.</span><span class="n">A</span><span class="o">.</span><span class="n">groupby</span><span class="p">((</span><span class="n">df</span><span class="o">.</span><span class="n">A</span> <span class="o">!=</span> <span class="n">df</span><span class="o">.</span><span class="n">A</span><span class="o">.</span><span class="n">shift</span><span class="p">())</span><span class="o">.</span><span class="n">cumsum</span><span class="p">())</span><span class="o">.</span><span class="n">groups</span>
<span class="gr">Out[126]: </span>
<span class="go">{1: Int64Index([0], dtype=&apos;int64&apos;),</span>
<span class="go"> 2: Int64Index([1], dtype=&apos;int64&apos;),</span>
<span class="go"> 3: Int64Index([2], dtype=&apos;int64&apos;),</span>
<span class="go"> 4: Int64Index([3, 4, 5], dtype=&apos;int64&apos;),</span>
<span class="go"> 5: Int64Index([6], dtype=&apos;int64&apos;),</span>
<span class="go"> 6: Int64Index([7, 8], dtype=&apos;int64&apos;)}</span>

<span class="gp">In [127]: </span><span class="n">df</span><span class="o">.</span><span class="n">A</span><span class="o">.</span><span class="n">groupby</span><span class="p">((</span><span class="n">df</span><span class="o">.</span><span class="n">A</span> <span class="o">!=</span> <span class="n">df</span><span class="o">.</span><span class="n">A</span><span class="o">.</span><span class="n">shift</span><span class="p">())</span><span class="o">.</span><span class="n">cumsum</span><span class="p">())</span><span class="o">.</span><span class="n">cumsum</span><span class="p">()</span>
<span class="gr">Out[127]: </span>
<span class="go">0    0</span>
<span class="go">1    1</span>
<span class="go">2    0</span>
<span class="go">3    1</span>
<span class="go">4    2</span>
<span class="go">5    3</span>
<span class="go">6    0</span>
<span class="go">7    1</span>
<span class="go">8    2</span>
<span class="go">Name: A, dtype: int64</span>
</pre></div>
</div>
<div class="section" id="expanding-data">
<h3><span class="yiyi-st" id="yiyi-174">Expanding Data</span></h3>
<p><span class="yiyi-st" id="yiyi-175"><a class="reference external" href="http://stackoverflow.com/questions/15489011/python-time-series-alignment-and-to-date-functions">对齐和最新</a></span></p>
<p><span class="yiyi-st" id="yiyi-176"><a class="reference external" href="http://stackoverflow.com/questions/14300768/pandas-rolling-computation-with-window-based-on-values-instead-of-counts">基于值而不是计数</a>滚动计算窗口</span></p>
<p><span class="yiyi-st" id="yiyi-177"><a class="reference external" href="http://stackoverflow.com/questions/15771472/pandas-rolling-mean-by-time-interval">按时间间隔滚动平均值</a></span></p>
</div>
<div class="section" id="id2">
<h3><span class="yiyi-st" id="yiyi-178">Splitting</span></h3>
<p><span class="yiyi-st" id="yiyi-179"><a class="reference external" href="http://stackoverflow.com/questions/13353233/best-way-to-split-a-dataframe-given-an-edge/15449992#15449992">拆分帧</a></span></p>
<p><span class="yiyi-st" id="yiyi-180">创建数据框列表，使用基于行中包含的逻辑的划分进行拆分。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [128]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="p">{</span><span class="s1">&apos;Case&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span><span class="s1">&apos;A&apos;</span><span class="p">,</span><span class="s1">&apos;A&apos;</span><span class="p">,</span><span class="s1">&apos;B&apos;</span><span class="p">,</span><span class="s1">&apos;A&apos;</span><span class="p">,</span><span class="s1">&apos;A&apos;</span><span class="p">,</span><span class="s1">&apos;B&apos;</span><span class="p">,</span><span class="s1">&apos;A&apos;</span><span class="p">,</span><span class="s1">&apos;A&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                         <span class="s1">&apos;Data&apos;</span> <span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">9</span><span class="p">)})</span>
<span class="gp">   .....:</span> 

<span class="gp">In [129]: </span><span class="n">dfs</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="o">*</span><span class="n">df</span><span class="o">.</span><span class="n">groupby</span><span class="p">((</span><span class="mi">1</span><span class="o">*</span><span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;Case&apos;</span><span class="p">]</span><span class="o">==</span><span class="s1">&apos;B&apos;</span><span class="p">))</span><span class="o">.</span><span class="n">cumsum</span><span class="p">()</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">window</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span><span class="n">min_periods</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">median</span><span class="p">())))[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>

<span class="gp">In [130]: </span><span class="n">dfs</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="gr">Out[130]: </span>
<span class="go">  Case      Data</span>
<span class="go">0    A  0.174068</span>
<span class="go">1    A -0.439461</span>
<span class="go">2    A -0.741343</span>
<span class="go">3    B -0.079673</span>

<span class="gp">In [131]: </span><span class="n">dfs</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="gr">Out[131]: </span>
<span class="go">  Case      Data</span>
<span class="go">4    A -0.922875</span>
<span class="go">5    A  0.303638</span>
<span class="go">6    B -0.917368</span>

<span class="gp">In [132]: </span><span class="n">dfs</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
<span class="gr">Out[132]: </span>
<span class="go">  Case      Data</span>
<span class="go">7    A -1.624062</span>
<span class="go">8    A -0.758514</span>
</pre></div>
</div>
</div>
<div class="section" id="pivot">
<span id="cookbook-pivot"></span><h3><span class="yiyi-st" id="yiyi-181">Pivot</span></h3>
<p><span class="yiyi-st" id="yiyi-182"><a class="reference internal" href="reshaping.html#reshaping-pivot"><span class="std std-ref">Pivot</span></a>文档。</span></p>
<p><span class="yiyi-st" id="yiyi-183"><a class="reference external" href="http://stackoverflow.com/questions/15570099/pandas-pivot-tables-row-subtotals/15574875#15574875">部分和和小计</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [133]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="p">{</span><span class="s1">&apos;Province&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="s1">&apos;ON&apos;</span><span class="p">,</span><span class="s1">&apos;QC&apos;</span><span class="p">,</span><span class="s1">&apos;BC&apos;</span><span class="p">,</span><span class="s1">&apos;AL&apos;</span><span class="p">,</span><span class="s1">&apos;AL&apos;</span><span class="p">,</span><span class="s1">&apos;MN&apos;</span><span class="p">,</span><span class="s1">&apos;ON&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                          <span class="s1">&apos;City&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="s1">&apos;Toronto&apos;</span><span class="p">,</span><span class="s1">&apos;Montreal&apos;</span><span class="p">,</span><span class="s1">&apos;Vancouver&apos;</span><span class="p">,</span><span class="s1">&apos;Calgary&apos;</span><span class="p">,</span><span class="s1">&apos;Edmonton&apos;</span><span class="p">,</span><span class="s1">&apos;Winnipeg&apos;</span><span class="p">,</span><span class="s1">&apos;Windsor&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                          <span class="s1">&apos;Sales&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">13</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">16</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">1</span><span class="p">]})</span>
<span class="gp">   .....:</span> 

<span class="gp">In [134]: </span><span class="n">table</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">pivot_table</span><span class="p">(</span><span class="n">df</span><span class="p">,</span><span class="n">values</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;Sales&apos;</span><span class="p">],</span><span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;Province&apos;</span><span class="p">],</span><span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;City&apos;</span><span class="p">],</span><span class="n">aggfunc</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">,</span><span class="n">margins</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="gp">In [135]: </span><span class="n">table</span><span class="o">.</span><span class="n">stack</span><span class="p">(</span><span class="s1">&apos;City&apos;</span><span class="p">)</span>
<span class="gr">Out[135]: </span>
<span class="go">                    Sales</span>
<span class="go">Province City            </span>
<span class="go">AL       All         12.0</span>
<span class="go">         Calgary      8.0</span>
<span class="go">         Edmonton     4.0</span>
<span class="go">BC       All         16.0</span>
<span class="go">         Vancouver   16.0</span>
<span class="go">MN       All          3.0</span>
<span class="go">         Winnipeg     3.0</span>
<span class="go">...                   ...</span>
<span class="go">All      Calgary      8.0</span>
<span class="go">         Edmonton     4.0</span>
<span class="go">         Montreal     6.0</span>
<span class="go">         Toronto     13.0</span>
<span class="go">         Vancouver   16.0</span>
<span class="go">         Windsor      1.0</span>
<span class="go">         Winnipeg     3.0</span>

<span class="go">[20 rows x 1 columns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-184"><a class="reference external" href="http://stackoverflow.com/questions/15589354/frequency-tables-in-pandas-like-plyr-in-r">频率表，例如R</a>中的plyr</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [136]: </span><span class="n">grades</span> <span class="o">=</span> <span class="p">[</span><span class="mi">48</span><span class="p">,</span><span class="mi">99</span><span class="p">,</span><span class="mi">75</span><span class="p">,</span><span class="mi">80</span><span class="p">,</span><span class="mi">42</span><span class="p">,</span><span class="mi">80</span><span class="p">,</span><span class="mi">72</span><span class="p">,</span><span class="mi">68</span><span class="p">,</span><span class="mi">36</span><span class="p">,</span><span class="mi">78</span><span class="p">]</span>

<span class="gp">In [137]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span> <span class="p">{</span><span class="s1">&apos;ID&apos;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;x</span><span class="si">%d</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">r</span> <span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)],</span>
<span class="gp">   .....:</span>                     <span class="s1">&apos;Gender&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="s1">&apos;F&apos;</span><span class="p">,</span> <span class="s1">&apos;M&apos;</span><span class="p">,</span> <span class="s1">&apos;F&apos;</span><span class="p">,</span> <span class="s1">&apos;M&apos;</span><span class="p">,</span> <span class="s1">&apos;F&apos;</span><span class="p">,</span> <span class="s1">&apos;M&apos;</span><span class="p">,</span> <span class="s1">&apos;F&apos;</span><span class="p">,</span> <span class="s1">&apos;M&apos;</span><span class="p">,</span> <span class="s1">&apos;M&apos;</span><span class="p">,</span> <span class="s1">&apos;M&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                     <span class="s1">&apos;ExamYear&apos;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&apos;2007&apos;</span><span class="p">,</span><span class="s1">&apos;2007&apos;</span><span class="p">,</span><span class="s1">&apos;2007&apos;</span><span class="p">,</span><span class="s1">&apos;2008&apos;</span><span class="p">,</span><span class="s1">&apos;2008&apos;</span><span class="p">,</span><span class="s1">&apos;2008&apos;</span><span class="p">,</span><span class="s1">&apos;2008&apos;</span><span class="p">,</span><span class="s1">&apos;2009&apos;</span><span class="p">,</span><span class="s1">&apos;2009&apos;</span><span class="p">,</span><span class="s1">&apos;2009&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                     <span class="s1">&apos;Class&apos;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&apos;algebra&apos;</span><span class="p">,</span> <span class="s1">&apos;stats&apos;</span><span class="p">,</span> <span class="s1">&apos;bio&apos;</span><span class="p">,</span> <span class="s1">&apos;algebra&apos;</span><span class="p">,</span> <span class="s1">&apos;algebra&apos;</span><span class="p">,</span> <span class="s1">&apos;stats&apos;</span><span class="p">,</span> <span class="s1">&apos;stats&apos;</span><span class="p">,</span> <span class="s1">&apos;algebra&apos;</span><span class="p">,</span> <span class="s1">&apos;bio&apos;</span><span class="p">,</span> <span class="s1">&apos;bio&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                     <span class="s1">&apos;Participated&apos;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&apos;yes&apos;</span><span class="p">,</span><span class="s1">&apos;yes&apos;</span><span class="p">,</span><span class="s1">&apos;yes&apos;</span><span class="p">,</span><span class="s1">&apos;yes&apos;</span><span class="p">,</span><span class="s1">&apos;no&apos;</span><span class="p">,</span><span class="s1">&apos;yes&apos;</span><span class="p">,</span><span class="s1">&apos;yes&apos;</span><span class="p">,</span><span class="s1">&apos;yes&apos;</span><span class="p">,</span><span class="s1">&apos;yes&apos;</span><span class="p">,</span><span class="s1">&apos;yes&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                     <span class="s1">&apos;Passed&apos;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&apos;yes&apos;</span> <span class="k">if</span> <span class="n">x</span> <span class="o">&gt;</span> <span class="mi">50</span> <span class="k">else</span> <span class="s1">&apos;no&apos;</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">grades</span><span class="p">],</span>
<span class="gp">   .....:</span>                     <span class="s1">&apos;Employed&apos;</span><span class="p">:</span> <span class="p">[</span><span class="bp">True</span><span class="p">,</span><span class="bp">True</span><span class="p">,</span><span class="bp">True</span><span class="p">,</span><span class="bp">False</span><span class="p">,</span><span class="bp">False</span><span class="p">,</span><span class="bp">False</span><span class="p">,</span><span class="bp">False</span><span class="p">,</span><span class="bp">True</span><span class="p">,</span><span class="bp">True</span><span class="p">,</span><span class="bp">False</span><span class="p">],</span>
<span class="gp">   .....:</span>                     <span class="s1">&apos;Grade&apos;</span><span class="p">:</span> <span class="n">grades</span><span class="p">})</span>
<span class="gp">   .....:</span> 

<span class="gp">In [138]: </span><span class="n">df</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="s1">&apos;ExamYear&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">agg</span><span class="p">({</span><span class="s1">&apos;Participated&apos;</span><span class="p">:</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">.</span><span class="n">value_counts</span><span class="p">()[</span><span class="s1">&apos;yes&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                     <span class="s1">&apos;Passed&apos;</span><span class="p">:</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">sum</span><span class="p">(</span><span class="n">x</span> <span class="o">==</span> <span class="s1">&apos;yes&apos;</span><span class="p">),</span>
<span class="gp">   .....:</span>                     <span class="s1">&apos;Employed&apos;</span> <span class="p">:</span> <span class="k">lambda</span> <span class="n">x</span> <span class="p">:</span> <span class="nb">sum</span><span class="p">(</span><span class="n">x</span><span class="p">),</span>
<span class="gp">   .....:</span>                     <span class="s1">&apos;Grade&apos;</span> <span class="p">:</span> <span class="k">lambda</span> <span class="n">x</span> <span class="p">:</span> <span class="nb">sum</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)})</span>
<span class="gp">   .....:</span> 
<span class="gr">Out[138]: </span>
<span class="go">          Grade  Employed  Participated  Passed</span>
<span class="go">ExamYear                                       </span>
<span class="go">2007         74         3             3       2</span>
<span class="go">2008         68         0             3       3</span>
<span class="go">2009         60         2             3       2</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-185"><a class="reference external" href="http://stackoverflow.com/questions/30379789/plot-pandas-data-frame-with-year-over-year-data">绘制pandas DataFrame与年度数据</a></span></p>
<p><span class="yiyi-st" id="yiyi-186">要创建年份和月份交叉表：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [139]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;value&apos;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">36</span><span class="p">)},</span>
<span class="gp">   .....:</span>                   <span class="n">index</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;2011-01-01&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;M&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">36</span><span class="p">))</span>
<span class="gp">   .....:</span> 

<span class="gp">In [140]: </span><span class="n">pd</span><span class="o">.</span><span class="n">pivot_table</span><span class="p">(</span><span class="n">df</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">year</span><span class="p">,</span>
<span class="gp">   .....:</span>                <span class="n">values</span><span class="o">=</span><span class="s1">&apos;value&apos;</span><span class="p">,</span> <span class="n">aggfunc</span><span class="o">=</span><span class="s1">&apos;sum&apos;</span><span class="p">)</span>
<span class="gp">   .....:</span> 
<span class="gr">Out[140]: </span>
<span class="go">        2011      2012      2013</span>
<span class="go">1  -0.560859  0.120930  0.516870</span>
<span class="go">2  -0.589005 -0.210518  0.343125</span>
<span class="go">3  -1.070678 -0.931184  2.137827</span>
<span class="go">4  -1.681101  0.240647  0.452429</span>
<span class="go">5   0.403776 -0.027462  0.483103</span>
<span class="go">6   0.609862  0.033113  0.061495</span>
<span class="go">7   0.387936 -0.658418  0.240767</span>
<span class="go">8   1.815066  0.324102  0.782413</span>
<span class="go">9   0.705200 -1.403048  0.628462</span>
<span class="go">10 -0.668049 -0.581967 -0.880627</span>
<span class="go">11  0.242501 -1.233862  0.777575</span>
<span class="go">12  0.313421 -3.520876 -0.779367</span>
</pre></div>
</div>
</div>
<div class="section" id="apply">
<h3><span class="yiyi-st" id="yiyi-187">Apply</span></h3>
<p><span class="yiyi-st" id="yiyi-188"><a class="reference external" href="http://stackoverflow.com/questions/17349981/converting-pandas-dataframe-with-categorical-values-into-binary-values">滚动应用于整理 - 将嵌入列表转换为多索引框架</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [141]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="p">{</span><span class="s1">&apos;A&apos;</span> <span class="p">:</span> <span class="p">[[</span><span class="mi">2</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">16</span><span class="p">],[</span><span class="mi">100</span><span class="p">,</span><span class="mi">200</span><span class="p">],[</span><span class="mi">10</span><span class="p">,</span><span class="mi">20</span><span class="p">,</span><span class="mi">30</span><span class="p">]],</span> <span class="s1">&apos;B&apos;</span> <span class="p">:</span> <span class="p">[[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span><span class="s1">&apos;b&apos;</span><span class="p">,</span><span class="s1">&apos;c&apos;</span><span class="p">],[</span><span class="s1">&apos;jj&apos;</span><span class="p">,</span><span class="s1">&apos;kk&apos;</span><span class="p">],[</span><span class="s1">&apos;ccc&apos;</span><span class="p">]]},</span><span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;I&apos;</span><span class="p">,</span><span class="s1">&apos;II&apos;</span><span class="p">,</span><span class="s1">&apos;III&apos;</span><span class="p">])</span>

<span class="gp">In [142]: </span><span class="k">def</span> <span class="nf">SeriesFromSubList</span><span class="p">(</span><span class="n">aList</span><span class="p">):</span>
<span class="gp">   .....:</span>    <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">aList</span><span class="p">)</span>
<span class="gp">   .....:</span> 

<span class="gp">In [143]: </span><span class="n">df_orgz</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">(</span><span class="nb">dict</span><span class="p">([</span> <span class="p">(</span><span class="n">ind</span><span class="p">,</span><span class="n">row</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">SeriesFromSubList</span><span class="p">))</span> <span class="k">for</span> <span class="n">ind</span><span class="p">,</span><span class="n">row</span> <span class="ow">in</span> <span class="n">df</span><span class="o">.</span><span class="n">iterrows</span><span class="p">()</span> <span class="p">]))</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-189"><a class="reference external" href="http://stackoverflow.com/questions/19121854/using-rolling-apply-on-a-dataframe-object">使用返回系列的DataFrame的滚动应用</a></span></p>
<p><span class="yiyi-st" id="yiyi-190">滚动应用于多个列，其中函数在返回系列的标量之前计算系列</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [144]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">2000</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span><span class="o">/</span><span class="mi">10000</span><span class="p">,</span>
<span class="gp">   .....:</span>                   <span class="n">index</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;2001-01-01&apos;</span><span class="p">,</span><span class="n">periods</span><span class="o">=</span><span class="mi">2000</span><span class="p">),</span>
<span class="gp">   .....:</span>                   <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span><span class="s1">&apos;B&apos;</span><span class="p">]);</span> <span class="n">df</span>
<span class="gp">   .....:</span> 
<span class="gr">Out[144]: </span>
<span class="go">                   A         B</span>
<span class="go">2001-01-01  0.000032 -0.000004</span>
<span class="go">2001-01-02 -0.000001  0.000207</span>
<span class="go">2001-01-03  0.000120 -0.000220</span>
<span class="go">2001-01-04 -0.000083 -0.000165</span>
<span class="go">2001-01-05 -0.000047  0.000156</span>
<span class="go">2001-01-06  0.000027  0.000104</span>
<span class="go">2001-01-07  0.000041 -0.000101</span>
<span class="go">...              ...       ...</span>
<span class="go">2006-06-17 -0.000034  0.000034</span>
<span class="go">2006-06-18  0.000002  0.000166</span>
<span class="go">2006-06-19  0.000023 -0.000081</span>
<span class="go">2006-06-20 -0.000061  0.000012</span>
<span class="go">2006-06-21 -0.000111  0.000027</span>
<span class="go">2006-06-22 -0.000061 -0.000009</span>
<span class="go">2006-06-23  0.000074 -0.000138</span>

<span class="go">[2000 rows x 2 columns]</span>

<span class="gp">In [145]: </span><span class="k">def</span> <span class="nf">gm</span><span class="p">(</span><span class="n">aDF</span><span class="p">,</span><span class="n">Const</span><span class="p">):</span>
<span class="gp">   .....:</span>    <span class="n">v</span> <span class="o">=</span> <span class="p">((((</span><span class="n">aDF</span><span class="o">.</span><span class="n">A</span><span class="o">+</span><span class="n">aDF</span><span class="o">.</span><span class="n">B</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">cumprod</span><span class="p">())</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="n">Const</span>
<span class="gp">   .....:</span>    <span class="k">return</span> <span class="p">(</span><span class="n">aDF</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="n">v</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="gp">   .....:</span> 

<span class="gp">In [146]: </span><span class="n">S</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="nb">dict</span><span class="p">([</span> <span class="n">gm</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">i</span><span class="p">:</span><span class="nb">min</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">51</span><span class="p">,</span><span class="nb">len</span><span class="p">(</span><span class="n">df</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">)],</span><span class="mi">5</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">df</span><span class="p">)</span><span class="o">-</span><span class="mi">50</span><span class="p">)</span> <span class="p">]));</span> <span class="n">S</span>
<span class="gr">Out[146]: </span>
<span class="go">2001-01-01   -0.001373</span>
<span class="go">2001-01-02   -0.001705</span>
<span class="go">2001-01-03   -0.002885</span>
<span class="go">2001-01-04   -0.002987</span>
<span class="go">2001-01-05   -0.002384</span>
<span class="go">2001-01-06   -0.004700</span>
<span class="go">2001-01-07   -0.005500</span>
<span class="go">                ...   </span>
<span class="go">2006-04-28   -0.002682</span>
<span class="go">2006-04-29   -0.002436</span>
<span class="go">2006-04-30   -0.002602</span>
<span class="go">2006-05-01   -0.001785</span>
<span class="go">2006-05-02   -0.001799</span>
<span class="go">2006-05-03   -0.000605</span>
<span class="go">2006-05-04   -0.000541</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-191"><a class="reference external" href="http://stackoverflow.com/questions/21040766/python-pandas-rolling-apply-two-column-input-into-function/21045831#21045831">使用返回标量</a>的DataFrame进行滚动应用</span></p>
<p><span class="yiyi-st" id="yiyi-192">滚动应用于多个列，其中函数返回标量（体积加权平均价格）</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [147]: </span><span class="n">rng</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="n">start</span> <span class="o">=</span> <span class="s1">&apos;2014-01-01&apos;</span><span class="p">,</span><span class="n">periods</span> <span class="o">=</span> <span class="mi">100</span><span class="p">)</span>

<span class="gp">In [148]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;Open&apos;</span> <span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">rng</span><span class="p">)),</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;Close&apos;</span> <span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">rng</span><span class="p">)),</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;Volume&apos;</span> <span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span><span class="mi">2000</span><span class="p">,</span><span class="nb">len</span><span class="p">(</span><span class="n">rng</span><span class="p">))},</span> <span class="n">index</span><span class="o">=</span><span class="n">rng</span><span class="p">);</span> <span class="n">df</span>
<span class="gp">   .....:</span> 
<span class="gr">Out[148]: </span>
<span class="go">               Close      Open  Volume</span>
<span class="go">2014-01-01 -0.653039  0.011174    1581</span>
<span class="go">2014-01-02  1.314205  0.214258    1707</span>
<span class="go">2014-01-03 -0.341915 -1.046922    1768</span>
<span class="go">2014-01-04 -1.303586 -0.752902     836</span>
<span class="go">2014-01-05  0.396288 -0.410793     694</span>
<span class="go">2014-01-06 -0.548006  0.648401     796</span>
<span class="go">2014-01-07  0.481380  0.737320     265</span>
<span class="go">...              ...       ...     ...</span>
<span class="go">2014-04-04 -2.548128  0.120378     564</span>
<span class="go">2014-04-05  0.223346  0.231661    1908</span>
<span class="go">2014-04-06  1.228841  0.952664    1090</span>
<span class="go">2014-04-07  0.552784 -0.176090    1813</span>
<span class="go">2014-04-08 -0.795389  1.781318    1103</span>
<span class="go">2014-04-09 -0.018815 -0.753493    1456</span>
<span class="go">2014-04-10  1.138197 -1.047997    1193</span>

<span class="go">[100 rows x 3 columns]</span>

<span class="gp">In [149]: </span><span class="k">def</span> <span class="nf">vwap</span><span class="p">(</span><span class="n">bars</span><span class="p">):</span> <span class="k">return</span> <span class="p">((</span><span class="n">bars</span><span class="o">.</span><span class="n">Close</span><span class="o">*</span><span class="n">bars</span><span class="o">.</span><span class="n">Volume</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span><span class="o">/</span><span class="n">bars</span><span class="o">.</span><span class="n">Volume</span><span class="o">.</span><span class="n">sum</span><span class="p">())</span>

<span class="gp">In [150]: </span><span class="n">window</span> <span class="o">=</span> <span class="mi">5</span>

<span class="gp">In [151]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span> <span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">vwap</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">i</span><span class="p">:</span><span class="n">i</span><span class="o">+</span><span class="n">window</span><span class="p">]),</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="n">window</span><span class="p">]]))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">df</span><span class="p">)</span><span class="o">-</span><span class="n">window</span><span class="p">)</span> <span class="p">]);</span>

<span class="gp">In [152]: </span><span class="n">s</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="gr">Out[152]: </span>
<span class="go">2014-01-06   -0.03</span>
<span class="go">2014-01-07    0.07</span>
<span class="go">2014-01-08   -0.40</span>
<span class="go">2014-01-09   -0.81</span>
<span class="go">2014-01-10   -0.63</span>
<span class="go">2014-01-11   -0.86</span>
<span class="go">2014-01-12   -0.36</span>
<span class="go">              ... </span>
<span class="go">2014-04-04   -1.27</span>
<span class="go">2014-04-05   -1.36</span>
<span class="go">2014-04-06   -0.73</span>
<span class="go">2014-04-07    0.04</span>
<span class="go">2014-04-08    0.21</span>
<span class="go">2014-04-09    0.07</span>
<span class="go">2014-04-10    0.25</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="timeseries">
<h2><span class="yiyi-st" id="yiyi-193">Timeseries</span></h2>
<p><span class="yiyi-st" id="yiyi-194"><a class="reference external" href="http://stackoverflow.com/questions/14539992/pandas-drop-rows-outside-of-time-range">时间</a></span></p>
<p><span class="yiyi-st" id="yiyi-195"><a class="reference external" href="http://stackoverflow.com/questions/17559885/pandas-dataframe-mask-based-on-index">在时间</a>之间使用索引器</span></p>
<p><span class="yiyi-st" id="yiyi-196"><a class="reference external" href="http://stackoverflow.com/questions/24010830/pandas-generate-sequential-timestamp-with-jump/24014440#24014440?">构造排除周末且仅包含特定时间的日期时间范围</a></span></p>
<p><span class="yiyi-st" id="yiyi-197"><a class="reference external" href="http://stackoverflow.com/questions/13893227/vectorized-look-up-of-values-in-pandas-dataframe">矢量化查找</a></span></p>
<p><span class="yiyi-st" id="yiyi-198"><a class="reference external" href="http://nipunbatra.github.io/2015/06/timeseries/">汇总和绘制时间序列</a></span></p>
<p><span class="yiyi-st" id="yiyi-199">将以列和天为单位的小时数的行转换为以时间序列形式的连续行序列。</span><span class="yiyi-st" id="yiyi-200"><a class="reference external" href="http://stackoverflow.com/questions/15432659/how-to-rearrange-a-python-pandas-dataframe">如何重新排列一个python pandas DataFrame？</a></span></p>
<p><span class="yiyi-st" id="yiyi-201"><a class="reference external" href="http://stackoverflow.com/questions/22244383/pandas-df-refill-adding-two-columns-of-different-shape">在将时间序列重新编入指定频率</a>时处理重复项</span></p>
<p><span class="yiyi-st" id="yiyi-202">计算DatetimeIndex中每个条目的月份第一天</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [153]: </span><span class="n">dates</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;2000-01-01&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>

<span class="gp">In [154]: </span><span class="n">dates</span><span class="o">.</span><span class="n">to_period</span><span class="p">(</span><span class="n">freq</span><span class="o">=</span><span class="s1">&apos;M&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">to_timestamp</span><span class="p">()</span>
<span class="gr">Out[154]: </span>
<span class="go">DatetimeIndex([&apos;2000-01-01&apos;, &apos;2000-01-01&apos;, &apos;2000-01-01&apos;, &apos;2000-01-01&apos;,</span>
<span class="go">               &apos;2000-01-01&apos;],</span>
<span class="go">              dtype=&apos;datetime64[ns]&apos;, freq=None)</span>
</pre></div>
</div>
<div class="section" id="resampling">
<span id="cookbook-resample"></span><h3><span class="yiyi-st" id="yiyi-203">Resampling</span></h3>
<p><span class="yiyi-st" id="yiyi-204"><a class="reference internal" href="timeseries.html#timeseries-resampling"><span class="std std-ref">Resample</span></a>文件。</span></p>
<p><span class="yiyi-st" id="yiyi-205"><a class="reference external" href="http://stackoverflow.com/questions/15297053/how-can-i-divide-single-values-of-a-dataframe-by-monthly-averages">按时间分组的值的时间分组</a></span></p>
<p><span class="yiyi-st" id="yiyi-206"><a class="reference external" href="http://stackoverflow.com/questions/14569223/timegrouper-pandas">TimeGrouping＃2</a></span></p>
<p><span class="yiyi-st" id="yiyi-207"><a class="reference external" href="https://github.com/pandas-dev/pandas/issues/3791">使用TimeGrouper和另一个分组来创建子组，然后应用自定义函数</a></span></p>
<p><span class="yiyi-st" id="yiyi-208"><a class="reference external" href="http://stackoverflow.com/questions/15408156/resampling-with-custom-periods">使用自定义周期重新采样</a></span></p>
<p><span class="yiyi-st" id="yiyi-209"><a class="reference external" href="http://stackoverflow.com/questions/14898574/resample-intrday-pandas-dataframe-without-add-new-days">重新取样日内栏框而不添加新日</a></span></p>
<p><span class="yiyi-st" id="yiyi-210"><a class="reference external" href="http://stackoverflow.com/questions/14861023/resampling-minute-data">重新采样分钟数据</a></span></p>
<p><span class="yiyi-st" id="yiyi-211"><a class="reference external" href="http://stackoverflow.com/q/18677271/564538">使用groupby重新取样</a></span></p>
</div>
</div>
<div class="section" id="merge">
<span id="cookbook-merge"></span><h2><span class="yiyi-st" id="yiyi-212">Merge</span></h2>
<p><span class="yiyi-st" id="yiyi-213"><a class="reference internal" href="merging.html#merging-concatenation"><span class="std std-ref">Concat</span></a>文档。</span><span class="yiyi-st" id="yiyi-214"><a class="reference internal" href="merging.html#merging-join"><span class="std std-ref">Join</span></a>文档。</span></p>
<p><span class="yiyi-st" id="yiyi-215"><a class="reference external" href="http://stackoverflow.com/questions/14988480/pandas-version-of-rbind">附加具有重叠索引的两个数据帧（模拟R rbind）</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [155]: </span><span class="n">rng</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;2000-01-01&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">6</span><span class="p">)</span>

<span class="gp">In [156]: </span><span class="n">df1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="n">rng</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">,</span> <span class="s1">&apos;C&apos;</span><span class="p">])</span>

<span class="gp">In [157]: </span><span class="n">df2</span> <span class="o">=</span> <span class="n">df1</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-216">在pandas中需要ignore_index</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [158]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">df1</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">df2</span><span class="p">,</span><span class="n">ignore_index</span><span class="o">=</span><span class="bp">True</span><span class="p">);</span> <span class="n">df</span>
<span class="gr">Out[158]: </span>
<span class="go">           A         B         C</span>
<span class="go">0  -0.480676 -1.305282 -0.212846</span>
<span class="go">1   1.979901  0.363112 -0.275732</span>
<span class="go">2  -1.433852  0.580237 -0.013672</span>
<span class="go">3   1.776623 -0.803467  0.521517</span>
<span class="go">4  -0.302508 -0.442948 -0.395768</span>
<span class="go">5  -0.249024 -0.031510  2.413751</span>
<span class="go">6  -0.480676 -1.305282 -0.212846</span>
<span class="go">7   1.979901  0.363112 -0.275732</span>
<span class="go">8  -1.433852  0.580237 -0.013672</span>
<span class="go">9   1.776623 -0.803467  0.521517</span>
<span class="go">10 -0.302508 -0.442948 -0.395768</span>
<span class="go">11 -0.249024 -0.031510  2.413751</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-217"><a class="reference external" href="https://github.com/pandas-dev/pandas/issues/2996">自加入DataFrame</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [159]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="p">{</span><span class="s1">&apos;Area&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">]</span> <span class="o">*</span> <span class="mi">5</span> <span class="o">+</span> <span class="p">[</span><span class="s1">&apos;C&apos;</span><span class="p">]</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span>
<span class="gp">   .....:</span>                         <span class="s1">&apos;Bins&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">110</span><span class="p">]</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="p">[</span><span class="mi">160</span><span class="p">]</span> <span class="o">*</span> <span class="mi">3</span> <span class="o">+</span> <span class="p">[</span><span class="mi">40</span><span class="p">]</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span>
<span class="gp">   .....:</span>                         <span class="s1">&apos;Test_0&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span>
<span class="gp">   .....:</span>                         <span class="s1">&apos;Data&apos;</span> <span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">7</span><span class="p">)});</span><span class="n">df</span>
<span class="gp">   .....:</span> 
<span class="gr">Out[159]: </span>
<span class="go">  Area  Bins      Data  Test_0</span>
<span class="go">0    A   110 -0.378914       0</span>
<span class="go">1    A   110 -1.032527       1</span>
<span class="go">2    A   160 -1.402816       0</span>
<span class="go">3    A   160  0.715333       1</span>
<span class="go">4    A   160 -0.091438       2</span>
<span class="go">5    C    40  1.608418       0</span>
<span class="go">6    C    40  0.753207       1</span>

<span class="gp">In [160]: </span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;Test_1&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&apos;Test_0&apos;</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span>

<span class="gp">In [161]: </span><span class="n">pd</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="n">df</span><span class="p">,</span> <span class="n">df</span><span class="p">,</span> <span class="n">left_on</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;Bins&apos;</span><span class="p">,</span> <span class="s1">&apos;Area&apos;</span><span class="p">,</span><span class="s1">&apos;Test_0&apos;</span><span class="p">],</span> <span class="n">right_on</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;Bins&apos;</span><span class="p">,</span> <span class="s1">&apos;Area&apos;</span><span class="p">,</span><span class="s1">&apos;Test_1&apos;</span><span class="p">],</span><span class="n">suffixes</span><span class="o">=</span><span class="p">(</span><span class="s1">&apos;_L&apos;</span><span class="p">,</span><span class="s1">&apos;_R&apos;</span><span class="p">))</span>
<span class="gr">Out[161]: </span>
<span class="go">  Area  Bins    Data_L  Test_0_L  Test_1_L    Data_R  Test_0_R  Test_1_R</span>
<span class="go">0    A   110 -0.378914         0        -1 -1.032527         1         0</span>
<span class="go">1    A   160 -1.402816         0        -1  0.715333         1         0</span>
<span class="go">2    A   160  0.715333         1         0 -0.091438         2         1</span>
<span class="go">3    C    40  1.608418         0        -1  0.753207         1         0</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-218"><a class="reference external" href="http://stackoverflow.com/questions/14341805/pandas-merge-pd-merge-how-to-set-the-index-and-join">如何设置索引并加入</a></span></p>
<p><span class="yiyi-st" id="yiyi-219"><a class="reference external" href="http://stackoverflow.com/questions/12322289/kdb-like-asof-join-for-timeseries-data-in-pandas/12336039#12336039">KDB like asof join</a></span></p>
<p><span class="yiyi-st" id="yiyi-220"><a class="reference external" href="http://stackoverflow.com/questions/15581829/how-to-perform-an-inner-or-outer-join-of-dataframes-with-pandas-on-non-simplisti">根据值</a>加入条件</span></p>
<p><span class="yiyi-st" id="yiyi-221"><a class="reference external" href="http://stackoverflow.com/questions/25125626/pandas-merge-with-logic/2512764">根据范围内的值使用searchsorted合并。</a></span></p>
</div>
<div class="section" id="plotting">
<span id="cookbook-plotting"></span><h2><span class="yiyi-st" id="yiyi-222">Plotting</span></h2>
<p><span class="yiyi-st" id="yiyi-223"><a class="reference internal" href="visualization.html#visualization"><span class="std std-ref">Plotting</span></a>文档。</span></p>
<p><span class="yiyi-st" id="yiyi-224"><a class="reference external" href="http://stackoverflow.com/questions/14349055/making-matplotlib-graphs-look-like-r-by-default">使Matplotlib看起来像R</a></span></p>
<p><span class="yiyi-st" id="yiyi-225"><a class="reference external" href="http://stackoverflow.com/questions/12945971/pandas-timeseries-plot-setting-x-axis-major-and-minor-ticks-and-labels">设置x轴主标签和次标签</a></span></p>
<p><span class="yiyi-st" id="yiyi-226"><a class="reference external" href="http://stackoverflow.com/questions/16392921/make-more-than-one-chart-in-same-ipython-notebook-cell">在ipython notebook中绘制多个图表</a></span></p>
<p><span class="yiyi-st" id="yiyi-227"><a class="reference external" href="http://stackoverflow.com/questions/16568964/make-a-multiline-plot-from-csv-file-in-matplotlib">创建多线图</a></span></p>
<p><span class="yiyi-st" id="yiyi-228"><a class="reference external" href="http://stackoverflow.com/questions/17050202/plot-timeseries-of-histograms-in-python">绘制热图</a></span></p>
<p><span class="yiyi-st" id="yiyi-229"><a class="reference external" href="http://stackoverflow.com/questions/11067368/annotate-time-series-plot-in-matplotlib">注释时间序列图</a></span></p>
<p><span class="yiyi-st" id="yiyi-230"><a class="reference external" href="http://stackoverflow.com/questions/17891493/annotating-points-from-a-pandas-dataframe-in-matplotlib-plot">注释时间序列图＃2</a></span></p>
<p><span class="yiyi-st" id="yiyi-231"><a class="reference external" href="https://pandas-xlsxwriter-charts.readthedocs.io/">使用Pandas，Vincent和xlsxwriter在excel文件中生成嵌入图。</a></span></p>
<p><span class="yiyi-st" id="yiyi-232"><a class="reference external" href="http://stackoverflow.com/questions/23232989/boxplot-stratified-by-column-in-python-pandas">分层变量</a>的四分位数的箱线图</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [162]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span>
<span class="gp">   .....:</span>      <span class="p">{</span><span class="s1">u&apos;stratifying_var&apos;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">20</span><span class="p">),</span>
<span class="gp">   .....:</span>       <span class="s1">u&apos;price&apos;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">20</span><span class="p">)})</span>
<span class="gp">   .....:</span> 

<span class="gp">In [163]: </span><span class="n">df</span><span class="p">[</span><span class="s1">u&apos;quartiles&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">qcut</span><span class="p">(</span>
<span class="gp">   .....:</span>     <span class="n">df</span><span class="p">[</span><span class="s1">u&apos;stratifying_var&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>     <span class="mi">4</span><span class="p">,</span>
<span class="gp">   .....:</span>     <span class="n">labels</span><span class="o">=</span><span class="p">[</span><span class="s1">u&apos;0-25%&apos;</span><span class="p">,</span> <span class="s1">u&apos;25-50%&apos;</span><span class="p">,</span> <span class="s1">u&apos;50-75%&apos;</span><span class="p">,</span> <span class="s1">u&apos;75-100%&apos;</span><span class="p">])</span>
<span class="gp">   .....:</span> 

<span class="gp">In [164]: </span><span class="n">df</span><span class="o">.</span><span class="n">boxplot</span><span class="p">(</span><span class="n">column</span><span class="o">=</span><span class="s1">u&apos;price&apos;</span><span class="p">,</span> <span class="n">by</span><span class="o">=</span><span class="s1">u&apos;quartiles&apos;</span><span class="p">)</span>
<span class="gr">Out[164]: </span><span class="o">&lt;</span><span class="n">matplotlib</span><span class="o">.</span><span class="n">axes</span><span class="o">.</span><span class="n">_subplots</span><span class="o">.</span><span class="n">AxesSubplot</span> <span class="n">at</span> <span class="mh">0x7ff27ea62b90</span><span class="o">&gt;</span>
</pre></div>
</div>
<img alt="http://pandas.pydata.org/pandas-docs/version/0.19.2/_images/quartile_boxplot.png" src="http://pandas.pydata.org/pandas-docs/version/0.19.2/_images/quartile_boxplot.png">
</div>
<div class="section" id="data-in-out">
<h2><span class="yiyi-st" id="yiyi-233">Data In/Out</span></h2>
<p><span class="yiyi-st" id="yiyi-234"><a class="reference external" href="http://stackoverflow.com/questions/16628329/hdf5-and-sqlite-concurrency-compression-i-o-performance">SQL与HDF5的性能比较</a></span></p>
<div class="section" id="csv">
<span id="cookbook-csv"></span><h3><span class="yiyi-st" id="yiyi-235">CSV</span></h3>
<p><span class="yiyi-st" id="yiyi-236"><a class="reference internal" href="io.html#io-read-csv-table"><span class="std std-ref">CSV</span></a>文件</span></p>
<p><span class="yiyi-st" id="yiyi-237"><a class="reference external" href="http://wesmckinney.com/blog/?p=635">read_csv in action</a></span></p>
<p><span class="yiyi-st" id="yiyi-238"><a class="reference external" href="http://stackoverflow.com/questions/17134942/pandas-dataframe-output-end-of-csv">写入csv</a></span></p>
<p><span class="yiyi-st" id="yiyi-239"><a class="reference external" href="http://stackoverflow.com/questions/25210819/speeding-up-data-import-function-pandas-and-appending-to-dataframe/25210900#25210900">如何读取多个文件，附加创建单个结构化数据</a></span></p>
<p><span class="yiyi-st" id="yiyi-240"><a class="reference external" href="http://stackoverflow.com/questions/11622652/large-persistent-dataframe-in-pandas/12193309#12193309">读取csv逐块读取</a></span></p>
<p><span class="yiyi-st" id="yiyi-241"><a class="reference external" href="http://stackoverflow.com/questions/19674212/pandas-data-frame-select-rows-and-clear-memory">只读取csv逐块的某些行</a></span></p>
<p><span class="yiyi-st" id="yiyi-242"><a class="reference external" href="http://stackoverflow.com/questions/15008970/way-to-read-first-few-lines-for-pandas-dataframe">读取结构数据的前几行</a></span></p>
<p><span class="yiyi-st" id="yiyi-243">读取被压缩但不是由<code class="docutils literal"><span class="pre">gzip/bz2</span></code>（<code class="docutils literal"><span class="pre">read_csv</span></code>理解的原生压缩格式）压缩的文件。</span><span class="yiyi-st" id="yiyi-244">此示例显示一个<code class="docutils literal"><span class="pre">WinZipped</span></code>文件，但是在上下文管理器中打开该文件并使用该句柄进行读取的一般应用程序。</span><span class="yiyi-st" id="yiyi-245"><a class="reference external" href="http://stackoverflow.com/questions/17789907/pandas-convert-winzipped-csv-file-to-data-frame">见这里</a></span></p>
<p><span class="yiyi-st" id="yiyi-246"><a class="reference external" href="http://stackoverflow.com/questions/15555005/get-inferred-dataframe-types-iteratively-using-chunksize">从文件中引用dtypes</a></span></p>
<p><span class="yiyi-st" id="yiyi-247"><a class="reference external" href="http://github.com/pandas-dev/pandas/issues/2886">处理数据损坏行</a></span></p>
<p><span class="yiyi-st" id="yiyi-248"><a class="reference external" href="http://nipunbatra.github.io/2013/06/reading-unclean-data-csv-using-pandas/">处理数据损坏行II</a></span></p>
<p><span class="yiyi-st" id="yiyi-249"><a class="reference external" href="http://nipunbatra.github.io/2013/06/pandas-reading-csv-with-unix-timestamps-and-converting-to-local-timezone/">使用Unix时间戳读取CSV并转换为本地时区</a></span></p>
<p><span class="yiyi-st" id="yiyi-250"><a class="reference external" href="http://stackoverflow.com/questions/17349574/pandas-write-multiindex-rows-with-to-csv">写入多行索引CSV，但不写入重复项</a></span></p>
<p><span class="yiyi-st" id="yiyi-251">使用格式解析多列中的日期组件更快</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">In</span> <span class="p">[</span><span class="mi">30</span><span class="p">]:</span> <span class="n">i</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;20000101&apos;</span><span class="p">,</span><span class="n">periods</span><span class="o">=</span><span class="mi">10000</span><span class="p">)</span>

<span class="n">In</span> <span class="p">[</span><span class="mi">31</span><span class="p">]:</span> <span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="n">year</span> <span class="o">=</span> <span class="n">i</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="n">month</span> <span class="o">=</span> <span class="n">i</span><span class="o">.</span><span class="n">month</span><span class="p">,</span> <span class="n">day</span> <span class="o">=</span> <span class="n">i</span><span class="o">.</span><span class="n">day</span><span class="p">))</span>

<span class="n">In</span> <span class="p">[</span><span class="mi">32</span><span class="p">]:</span> <span class="n">df</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
<span class="n">Out</span><span class="p">[</span><span class="mi">32</span><span class="p">]:</span>
   <span class="n">day</span>  <span class="n">month</span>  <span class="n">year</span>
<span class="mi">0</span>    <span class="mi">1</span>      <span class="mi">1</span>  <span class="mi">2000</span>
<span class="mi">1</span>    <span class="mi">2</span>      <span class="mi">1</span>  <span class="mi">2000</span>
<span class="mi">2</span>    <span class="mi">3</span>      <span class="mi">1</span>  <span class="mi">2000</span>
<span class="mi">3</span>    <span class="mi">4</span>      <span class="mi">1</span>  <span class="mi">2000</span>
<span class="mi">4</span>    <span class="mi">5</span>      <span class="mi">1</span>  <span class="mi">2000</span>

<span class="n">In</span> <span class="p">[</span><span class="mi">33</span><span class="p">]:</span> <span class="o">%</span><span class="n">timeit</span> <span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">year</span><span class="o">*</span><span class="mi">10000</span><span class="o">+</span><span class="n">df</span><span class="o">.</span><span class="n">month</span><span class="o">*</span><span class="mi">100</span><span class="o">+</span><span class="n">df</span><span class="o">.</span><span class="n">day</span><span class="p">,</span><span class="n">format</span><span class="o">=</span><span class="s1">&apos;%Y%m</span><span class="si">%d</span><span class="s1">&apos;</span><span class="p">)</span>
<span class="mi">100</span> <span class="n">loops</span><span class="p">,</span> <span class="n">best</span> <span class="n">of</span> <span class="mi">3</span><span class="p">:</span> <span class="mf">7.08</span> <span class="n">ms</span> <span class="n">per</span> <span class="n">loop</span>

<span class="c1"># simulate combinging into a string, then parsing</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">34</span><span class="p">]:</span> <span class="n">ds</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="s2">&quot;</span><span class="si">%04d%02d%02d</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="s1">&apos;year&apos;</span><span class="p">],</span><span class="n">x</span><span class="p">[</span><span class="s1">&apos;month&apos;</span><span class="p">],</span><span class="n">x</span><span class="p">[</span><span class="s1">&apos;day&apos;</span><span class="p">]),</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

<span class="n">In</span> <span class="p">[</span><span class="mi">35</span><span class="p">]:</span> <span class="n">ds</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
<span class="n">Out</span><span class="p">[</span><span class="mi">35</span><span class="p">]:</span>
<span class="mi">0</span>    <span class="mi">20000101</span>
<span class="mi">1</span>    <span class="mi">20000102</span>
<span class="mi">2</span>    <span class="mi">20000103</span>
<span class="mi">3</span>    <span class="mi">20000104</span>
<span class="mi">4</span>    <span class="mi">20000105</span>
<span class="n">dtype</span><span class="p">:</span> <span class="nb">object</span>

<span class="n">In</span> <span class="p">[</span><span class="mi">36</span><span class="p">]:</span> <span class="o">%</span><span class="n">timeit</span> <span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">ds</span><span class="p">)</span>
<span class="mi">1</span> <span class="n">loops</span><span class="p">,</span> <span class="n">best</span> <span class="n">of</span> <span class="mi">3</span><span class="p">:</span> <span class="mi">488</span> <span class="n">ms</span> <span class="n">per</span> <span class="n">loop</span>
</pre></div>
</div>
<div class="section" id="skip-row-between-header-and-data">
<h4><span class="yiyi-st" id="yiyi-252">Skip row between header and data</span></h4>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [165]: </span><span class="kn">from</span> <span class="nn">io</span> <span class="kn">import</span> <span class="n">StringIO</span>

<span class="gp">In [166]: </span><span class="kn">import</span> <span class="nn">pandas</span> <span class="kn">as</span> <span class="nn">pd</span>

<span class="gp">In [167]: </span><span class="n">data</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;;;;;</span>
<span class="gp">   .....:</span><span class="s2">  ;;;;</span>
<span class="gp">   .....:</span><span class="s2">  ;;;;</span>
<span class="gp">   .....:</span><span class="s2">  ;;;;</span>
<span class="gp">   .....:</span><span class="s2">  ;;;;</span>
<span class="gp">   .....:</span><span class="s2">  ;;;;</span>
<span class="gp">   .....:</span><span class="s2"> ;;;;</span>
<span class="gp">   .....:</span><span class="s2">  ;;;;</span>
<span class="gp">   .....:</span><span class="s2">  ;;;;</span>
<span class="gp">   .....:</span><span class="s2"> ;;;;</span>
<span class="gp">   .....:</span><span class="s2"> date;Param1;Param2;Param4;Param5</span>
<span class="gp">   .....:</span><span class="s2">     ;m&#xB2;;&#xB0;C;m&#xB2;;m</span>
<span class="gp">   .....:</span><span class="s2"> ;;;;</span>
<span class="gp">   .....:</span><span class="s2"> 01.01.1990 00:00;1;1;2;3</span>
<span class="gp">   .....:</span><span class="s2"> 01.01.1990 01:00;5;3;4;5</span>
<span class="gp">   .....:</span><span class="s2"> 01.01.1990 02:00;9;5;6;7</span>
<span class="gp">   .....:</span><span class="s2"> 01.01.1990 03:00;13;7;8;9</span>
<span class="gp">   .....:</span><span class="s2"> 01.01.1990 04:00;17;9;10;11</span>
<span class="gp">   .....:</span><span class="s2"> 01.01.1990 05:00;21;11;12;13</span>
<span class="gp">   .....:</span><span class="s2"> &quot;&quot;&quot;</span>
<span class="gp">   .....:</span> 
</pre></div>
</div>
<div class="section" id="option-1-pass-rows-explicitly-to-skiprows">
<h5><span class="yiyi-st" id="yiyi-253">Option 1: pass rows explicitly to skiprows</span></h5>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [168]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&apos;UTF-8&apos;</span><span class="p">)),</span> <span class="n">sep</span><span class="o">=</span><span class="s1">&apos;;&apos;</span><span class="p">,</span> <span class="n">skiprows</span><span class="o">=</span><span class="p">[</span><span class="mi">11</span><span class="p">,</span><span class="mi">12</span><span class="p">],</span>
<span class="gp">   .....:</span>         <span class="n">index_col</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">parse_dates</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
<span class="gp">   .....:</span> 
<span class="gr">Out[168]: </span>
<span class="go">                     Param1  Param2  Param4  Param5</span>
<span class="go">date                                               </span>
<span class="go">1990-01-01 00:00:00       1       1       2       3</span>
<span class="go">1990-01-01 01:00:00       5       3       4       5</span>
<span class="go">1990-01-01 02:00:00       9       5       6       7</span>
<span class="go">1990-01-01 03:00:00      13       7       8       9</span>
<span class="go">1990-01-01 04:00:00      17       9      10      11</span>
<span class="go">1990-01-01 05:00:00      21      11      12      13</span>
</pre></div>
</div>
</div>
<div class="section" id="option-2-read-column-names-and-then-data">
<h5><span class="yiyi-st" id="yiyi-254">Option 2: read column names and then data</span></h5>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [169]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&apos;UTF-8&apos;</span><span class="p">)),</span> <span class="n">sep</span><span class="o">=</span><span class="s1">&apos;;&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>         <span class="n">header</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">parse_dates</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">nrows</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span><span class="o">.</span><span class="n">columns</span>
<span class="gp">   .....:</span> 
<span class="gr">Out[169]: </span><span class="n">Index</span><span class="p">([</span><span class="s1">u&apos;date&apos;</span><span class="p">,</span> <span class="s1">u&apos;Param1&apos;</span><span class="p">,</span> <span class="s1">u&apos;Param2&apos;</span><span class="p">,</span> <span class="s1">u&apos;Param4&apos;</span><span class="p">,</span> <span class="s1">u&apos;Param5&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;object&apos;</span><span class="p">)</span>

<span class="gp">In [170]: </span><span class="n">columns</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&apos;UTF-8&apos;</span><span class="p">)),</span> <span class="n">sep</span><span class="o">=</span><span class="s1">&apos;;&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>                   <span class="n">header</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">parse_dates</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">nrows</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span><span class="o">.</span><span class="n">columns</span>
<span class="gp">   .....:</span> 

<span class="gp">In [171]: </span><span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">StringIO</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&apos;UTF-8&apos;</span><span class="p">)),</span> <span class="n">sep</span><span class="o">=</span><span class="s1">&apos;;&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>             <span class="n">header</span><span class="o">=</span><span class="mi">12</span><span class="p">,</span> <span class="n">parse_dates</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">names</span><span class="o">=</span><span class="n">columns</span><span class="p">)</span>
<span class="gp">   .....:</span> 
<span class="gr">Out[171]: </span>
<span class="go">               date  Param1  Param2  Param4  Param5</span>
<span class="go">0  01.01.1990 00:00       1       1       2       3</span>
<span class="go">1  01.01.1990 01:00       5       3       4       5</span>
<span class="go">2  01.01.1990 02:00       9       5       6       7</span>
<span class="go">3  01.01.1990 03:00      13       7       8       9</span>
<span class="go">4  01.01.1990 04:00      17       9      10      11</span>
<span class="go">5  01.01.1990 05:00      21      11      12      13</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="section" id="sql">
<span id="cookbook-sql"></span><h3><span class="yiyi-st" id="yiyi-255">SQL</span></h3>
<p><span class="yiyi-st" id="yiyi-256"><a class="reference internal" href="io.html#io-sql"><span class="std std-ref">SQL</span></a>文档</span></p>
<p><span class="yiyi-st" id="yiyi-257"><a class="reference external" href="http://stackoverflow.com/questions/10065051/python-pandas-and-databases-like-mysql">使用SQL从数据库读取</a></span></p>
</div>
<div class="section" id="excel">
<span id="cookbook-excel"></span><h3><span class="yiyi-st" id="yiyi-258">Excel</span></h3>
<p><span class="yiyi-st" id="yiyi-259"><a class="reference internal" href="io.html#io-excel"><span class="std std-ref">Excel</span></a>文档</span></p>
<p><span class="yiyi-st" id="yiyi-260"><a class="reference external" href="http://stackoverflow.com/questions/15588713/sheets-of-excel-workbook-from-a-url-into-a-pandas-dataframe">从文件状句柄读取</a></span></p>
<p><span class="yiyi-st" id="yiyi-261"><a class="reference external" href="http://pbpython.com/improve-pandas-excel-output.html">在XlsxWriter输出中修改格式设置</a></span></p>
</div>
<div class="section" id="html">
<span id="cookbook-html"></span><h3><span class="yiyi-st" id="yiyi-262">HTML</span></h3>
<p><span class="yiyi-st" id="yiyi-263"><a class="reference external" href="http://stackoverflow.com/a/18939272/564538">从无法处理默认请求标头</a>的服务器读取HTML表格</span></p>
</div>
<div class="section" id="hdfstore">
<span id="cookbook-hdf"></span><h3><span class="yiyi-st" id="yiyi-264">HDFStore</span></h3>
<p><span class="yiyi-st" id="yiyi-265"><a class="reference internal" href="io.html#io-hdf5"><span class="std std-ref">HDFStores</span></a>文档</span></p>
<p><span class="yiyi-st" id="yiyi-266"><a class="reference external" href="http://stackoverflow.com/questions/13926089/selecting-columns-from-pandas-hdfstore-table">具有时间戳索引的简单查询</a></span></p>
<p><span class="yiyi-st" id="yiyi-267"><a class="reference external" href="http://github.com/pandas-dev/pandas/issues/3032">使用链接的多表层次结构管理异构数据</a></span></p>
<p><span class="yiyi-st" id="yiyi-268"><a class="reference external" href="http://stackoverflow.com/questions/14614512/merging-two-tables-with-millions-of-rows-in-python/14617925#14617925">合并数百万行的磁盘表</a></span></p>
<p><span class="yiyi-st" id="yiyi-269"><a class="reference external" href="http://stackoverflow.com/a/29014295/2858145">在从多个进程/线程写入商店时避免不一致</a></span></p>
<p><span class="yiyi-st" id="yiyi-270">通过块去重复大型存储，本质上是递归归约运算。</span><span class="yiyi-st" id="yiyi-271">显示从csv文件接收数据并通过块创建存储的功能，以及日期解析。</span><span class="yiyi-st" id="yiyi-272"><a class="reference external" href="http://stackoverflow.com/questions/16110252/need-to-compare-very-large-files-around-1-5gb-in-python/16110391#16110391">见这里</a></span></p>
<p><span class="yiyi-st" id="yiyi-273"><a class="reference external" href="http://stackoverflow.com/questions/20428355/appending-column-to-frame-of-hdf-file-in-pandas/20428786#20428786">从csv文件创建存储块chunk-by-chunk</a></span></p>
<p><span class="yiyi-st" id="yiyi-274"><a class="reference external" href="http://stackoverflow.com/questions/16997048/how-does-one-append-large-amounts-of-data-to-a-pandas-hdfstore-and-get-a-natural/16999397#16999397">附加到商店，同时创建唯一索引</a></span></p>
<p><span class="yiyi-st" id="yiyi-275"><a class="reference external" href="http://stackoverflow.com/questions/14262433/large-data-work-flows-using-pandas">大数据工作流</a></span></p>
<p><span class="yiyi-st" id="yiyi-276"><a class="reference external" href="http://stackoverflow.com/questions/16997048/how-does-one-append-large-amounts-of-data-to-a-pandas-hdfstore-and-get-a-natural">读取文件序列，然后在附加</a>时为商店提供全局唯一索引</span></p>
<p><span class="yiyi-st" id="yiyi-277"><a class="reference external" href="http://stackoverflow.com/questions/15798209/pandas-group-by-query-on-large-data-in-hdfstore">Groupby在具有低组密度的HDFStore上</a></span></p>
<p><span class="yiyi-st" id="yiyi-278"><a class="reference external" href="http://stackoverflow.com/questions/25459982/trouble-with-grouby-on-millions-of-keys-on-a-chunked-file-in-python-pandas/25471765#25471765">Groupby在具有高组密度的HDFStore上</a></span></p>
<p><span class="yiyi-st" id="yiyi-279"><a class="reference external" href="http://stackoverflow.com/questions/22777284/improve-query-performance-from-a-large-hdfstore-table-with-pandas/22820780#22820780">HDFStore上的分层查询</a></span></p>
<p><span class="yiyi-st" id="yiyi-280"><a class="reference external" href="http://stackoverflow.com/questions/20497897/converting-dict-of-dicts-into-pandas-dataframe-memory-issues">使用HDFStore计数</a></span></p>
<p><span class="yiyi-st" id="yiyi-281"><a class="reference external" href="http://stackoverflow.com/questions/15488809/how-to-trouble-shoot-hdfstore-exception-cannot-find-the-correct-atom-type">对HDFStore异常进行故障排除</a></span></p>
<p><span class="yiyi-st" id="yiyi-282"><a class="reference external" href="http://stackoverflow.com/questions/15988871/hdfstore-appendstring-dataframe-fails-when-string-column-contents-are-longer">用字符串设置min_itemsize</a></span></p>
<p><span class="yiyi-st" id="yiyi-283"><a class="reference external" href="http://stackoverflow.com/questions/17893370/ptrepack-sortby-needs-full-index">使用ptrepack在商店上创建完全排序索引</a></span></p>
<p><span class="yiyi-st" id="yiyi-284">将属性存储到组节点</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [172]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">8</span><span class="p">,</span><span class="mi">3</span><span class="p">))</span>

<span class="gp">In [173]: </span><span class="n">store</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">HDFStore</span><span class="p">(</span><span class="s1">&apos;test.h5&apos;</span><span class="p">)</span>

<span class="gp">In [174]: </span><span class="n">store</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="s1">&apos;df&apos;</span><span class="p">,</span><span class="n">df</span><span class="p">)</span>

<span class="c"># you can store an arbitrary python object via pickle</span>
<span class="gp">In [175]: </span><span class="n">store</span><span class="o">.</span><span class="n">get_storer</span><span class="p">(</span><span class="s1">&apos;df&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">attrs</span><span class="o">.</span><span class="n">my_attribute</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">A</span> <span class="o">=</span> <span class="mi">10</span><span class="p">)</span>

<span class="gp">In [176]: </span><span class="n">store</span><span class="o">.</span><span class="n">get_storer</span><span class="p">(</span><span class="s1">&apos;df&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">attrs</span><span class="o">.</span><span class="n">my_attribute</span>
<span class="gr">Out[176]: </span><span class="p">{</span><span class="s1">&apos;A&apos;</span><span class="p">:</span> <span class="mi">10</span><span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="binary-files">
<span id="cookbook-binary"></span><h3><span class="yiyi-st" id="yiyi-285">Binary Files</span></h3>
<p><span class="yiyi-st" id="yiyi-286">pandas很容易接受numpy记录数组，如果你需要读入一个由C结构数组组成的二进制文件。</span><span class="yiyi-st" id="yiyi-287">例如，给定这个C程序在用<code class="docutils literal"><span class="pre">gcc</span> <span class="pre">main.c</span> <span class="pre">编译的<code class="docutils literal"><span class="pre">main.c</span></code>文件中-std = gnu99</span></code>在64位机器上，</span></p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&lt;stdio.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;stdint.h&gt;</span><span class="cp"></span>

<span class="k">typedef</span> <span class="k">struct</span> <span class="n">_Data</span>
<span class="p">{</span>
    <span class="kt">int32_t</span> <span class="n">count</span><span class="p">;</span>
    <span class="kt">double</span> <span class="n">avg</span><span class="p">;</span>
    <span class="kt">float</span> <span class="n">scale</span><span class="p">;</span>
<span class="p">}</span> <span class="n">Data</span><span class="p">;</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">argv</span><span class="p">[])</span>
<span class="p">{</span>
    <span class="kt">size_t</span> <span class="n">n</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>
    <span class="n">Data</span> <span class="n">d</span><span class="p">[</span><span class="n">n</span><span class="p">];</span>

    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">d</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">count</span> <span class="o">=</span> <span class="n">i</span><span class="p">;</span>
        <span class="n">d</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">avg</span> <span class="o">=</span> <span class="n">i</span> <span class="o">+</span> <span class="mf">1.0</span><span class="p">;</span>
        <span class="n">d</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">scale</span> <span class="o">=</span> <span class="p">(</span><span class="kt">float</span><span class="p">)</span> <span class="n">i</span> <span class="o">+</span> <span class="mf">2.0f</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="kt">FILE</span> <span class="o">*</span><span class="n">file</span> <span class="o">=</span> <span class="n">fopen</span><span class="p">(</span><span class="s">&quot;binary.dat&quot;</span><span class="p">,</span> <span class="s">&quot;wb&quot;</span><span class="p">);</span>
    <span class="n">fwrite</span><span class="p">(</span><span class="o">&amp;</span><span class="n">d</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">Data</span><span class="p">),</span> <span class="n">n</span><span class="p">,</span> <span class="n">file</span><span class="p">);</span>
    <span class="n">fclose</span><span class="p">(</span><span class="n">file</span><span class="p">);</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-288">以下Python代码将二进制文件<code class="docutils literal"><span class="pre">&apos;binary.dat&apos;</span></code>读入pandas <code class="docutils literal"><span class="pre">DataFrame</span></code>，其中结构的每个元素对应于框架中的一列：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">names</span> <span class="o">=</span> <span class="s1">&apos;count&apos;</span><span class="p">,</span> <span class="s1">&apos;avg&apos;</span><span class="p">,</span> <span class="s1">&apos;scale&apos;</span>

<span class="c1"># note that the offsets are larger than the size of the type because of</span>
<span class="c1"># struct padding</span>
<span class="n">offsets</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">16</span>
<span class="n">formats</span> <span class="o">=</span> <span class="s1">&apos;i4&apos;</span><span class="p">,</span> <span class="s1">&apos;f8&apos;</span><span class="p">,</span> <span class="s1">&apos;f4&apos;</span>
<span class="n">dt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">({</span><span class="s1">&apos;names&apos;</span><span class="p">:</span> <span class="n">names</span><span class="p">,</span> <span class="s1">&apos;offsets&apos;</span><span class="p">:</span> <span class="n">offsets</span><span class="p">,</span> <span class="s1">&apos;formats&apos;</span><span class="p">:</span> <span class="n">formats</span><span class="p">},</span>
              <span class="n">align</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">fromfile</span><span class="p">(</span><span class="s1">&apos;binary.dat&apos;</span><span class="p">,</span> <span class="n">dt</span><span class="p">))</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-289">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-290">结构元素的偏移量可以根据其上创建文件的机器的体系结构而不同。</span><span class="yiyi-st" id="yiyi-291">不推荐使用这样的原始二进制文件格式进行一般数据存储，因为它不是跨平台。</span><span class="yiyi-st" id="yiyi-292">我们建议使用HDF5或msgpack，这两个都由pandas的IO设备支持。</span></p>
</div>
</div>
</div>
<div class="section" id="computation">
<h2><span class="yiyi-st" id="yiyi-293">Computation</span></h2>
<p><span class="yiyi-st" id="yiyi-294"><a class="reference external" href="http://nbviewer.ipython.org/5720498">时间序列</a>的数值积分（基于样本）</span></p>
</div>
<div class="section" id="timedeltas">
<h2><span class="yiyi-st" id="yiyi-295">Timedeltas</span></h2>
<p><span class="yiyi-st" id="yiyi-296"><a class="reference internal" href="timedeltas.html#timedeltas-timedeltas"><span class="std std-ref">Timedeltas</span></a>文件。</span></p>
<p><span class="yiyi-st" id="yiyi-297"><a class="reference external" href="http://github.com/pandas-dev/pandas/pull/2899">使用timedeltas</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [177]: </span><span class="n">s</span>  <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;2012-1-1&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">))</span>

<span class="gp">In [178]: </span><span class="n">s</span> <span class="o">-</span> <span class="n">s</span><span class="o">.</span><span class="n">max</span><span class="p">()</span>
<span class="gr">Out[178]: </span>
<span class="go">0   -2 days</span>
<span class="go">1   -1 days</span>
<span class="go">2    0 days</span>
<span class="go">dtype: timedelta64[ns]</span>

<span class="gp">In [179]: </span><span class="n">s</span><span class="o">.</span><span class="n">max</span><span class="p">()</span> <span class="o">-</span> <span class="n">s</span>
<span class="gr">Out[179]: </span>
<span class="go">0   2 days</span>
<span class="go">1   1 days</span>
<span class="go">2   0 days</span>
<span class="go">dtype: timedelta64[ns]</span>

<span class="gp">In [180]: </span><span class="n">s</span> <span class="o">-</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2011</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">5</span><span class="p">)</span>
<span class="gr">Out[180]: </span>
<span class="go">0   364 days 20:55:00</span>
<span class="go">1   365 days 20:55:00</span>
<span class="go">2   366 days 20:55:00</span>
<span class="go">dtype: timedelta64[ns]</span>

<span class="gp">In [181]: </span><span class="n">s</span> <span class="o">+</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="n">minutes</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="gr">Out[181]: </span>
<span class="go">0   2012-01-01 00:05:00</span>
<span class="go">1   2012-01-02 00:05:00</span>
<span class="go">2   2012-01-03 00:05:00</span>
<span class="go">dtype: datetime64[ns]</span>

<span class="gp">In [182]: </span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2011</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">5</span><span class="p">)</span> <span class="o">-</span> <span class="n">s</span>
<span class="gr">Out[182]: </span>
<span class="go">0   -365 days +03:05:00</span>
<span class="go">1   -366 days +03:05:00</span>
<span class="go">2   -367 days +03:05:00</span>
<span class="go">dtype: timedelta64[ns]</span>

<span class="gp">In [183]: </span><span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="n">minutes</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span> <span class="o">+</span> <span class="n">s</span>
<span class="gr">Out[183]: </span>
<span class="go">0   2012-01-01 00:05:00</span>
<span class="go">1   2012-01-02 00:05:00</span>
<span class="go">2   2012-01-03 00:05:00</span>
<span class="go">dtype: datetime64[ns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-298"><a class="reference external" href="http://stackoverflow.com/questions/16385785/add-days-to-dates-in-dataframe">添加和减去三角形和日期</a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [184]: </span><span class="n">deltas</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="p">])</span>

<span class="gp">In [185]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="n">A</span> <span class="o">=</span> <span class="n">s</span><span class="p">,</span> <span class="n">B</span> <span class="o">=</span> <span class="n">deltas</span><span class="p">));</span> <span class="n">df</span>
<span class="gr">Out[185]: </span>
<span class="go">           A      B</span>
<span class="go">0 2012-01-01 0 days</span>
<span class="go">1 2012-01-02 1 days</span>
<span class="go">2 2012-01-03 2 days</span>

<span class="gp">In [186]: </span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;New Dates&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">]</span> <span class="o">+</span> <span class="n">df</span><span class="p">[</span><span class="s1">&apos;B&apos;</span><span class="p">];</span>

<span class="gp">In [187]: </span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;Delta&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">]</span> <span class="o">-</span> <span class="n">df</span><span class="p">[</span><span class="s1">&apos;New Dates&apos;</span><span class="p">];</span> <span class="n">df</span>
<span class="gr">Out[187]: </span>
<span class="go">           A      B  New Dates   Delta</span>
<span class="go">0 2012-01-01 0 days 2012-01-01  0 days</span>
<span class="go">1 2012-01-02 1 days 2012-01-03 -1 days</span>
<span class="go">2 2012-01-03 2 days 2012-01-05 -2 days</span>

<span class="gp">In [188]: </span><span class="n">df</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[188]: </span>
<span class="go">A             datetime64[ns]</span>
<span class="go">B            timedelta64[ns]</span>
<span class="go">New Dates     datetime64[ns]</span>
<span class="go">Delta        timedelta64[ns]</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-299"><a class="reference external" href="http://stackoverflow.com/questions/15683588/iterating-through-a-pandas-dataframe">另一个例子</a></span></p>
<p><span class="yiyi-st" id="yiyi-300">值可以使用np.nan设置为NaT，类似于datetime</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [189]: </span><span class="n">y</span> <span class="o">=</span> <span class="n">s</span> <span class="o">-</span> <span class="n">s</span><span class="o">.</span><span class="n">shift</span><span class="p">();</span> <span class="n">y</span>
<span class="gr">Out[189]: </span>
<span class="go">0      NaT</span>
<span class="go">1   1 days</span>
<span class="go">2   1 days</span>
<span class="go">dtype: timedelta64[ns]</span>

<span class="gp">In [190]: </span><span class="n">y</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">;</span> <span class="n">y</span>
<span class="gr">Out[190]: </span>
<span class="go">0      NaT</span>
<span class="go">1      NaT</span>
<span class="go">2   1 days</span>
<span class="go">dtype: timedelta64[ns]</span>
</pre></div>
</div>
</div>
<div class="section" id="aliasing-axis-names">
<h2><span class="yiyi-st" id="yiyi-301">Aliasing Axis Names</span></h2>
<p><span class="yiyi-st" id="yiyi-302">要全局提供轴名称的别名，可以定义这两个函数：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [191]: </span><span class="k">def</span> <span class="nf">set_axis_alias</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">axis</span><span class="p">,</span> <span class="n">alias</span><span class="p">):</span>
<span class="gp">   .....:</span>    <span class="k">if</span> <span class="n">axis</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">cls</span><span class="o">.</span><span class="n">_AXIS_NUMBERS</span><span class="p">:</span>
<span class="gp">   .....:</span>       <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s2">&quot;invalid axis [</span><span class="si">%s</span><span class="s2">] for alias [</span><span class="si">%s</span><span class="s2">]&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">axis</span><span class="p">,</span> <span class="n">alias</span><span class="p">))</span>
<span class="gp">   .....:</span>    <span class="n">cls</span><span class="o">.</span><span class="n">_AXIS_ALIASES</span><span class="p">[</span><span class="n">alias</span><span class="p">]</span> <span class="o">=</span> <span class="n">axis</span>
<span class="gp">   .....:</span> 
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [192]: </span><span class="k">def</span> <span class="nf">clear_axis_alias</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">axis</span><span class="p">,</span> <span class="n">alias</span><span class="p">):</span>
<span class="gp">   .....:</span>    <span class="k">if</span> <span class="n">axis</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">cls</span><span class="o">.</span><span class="n">_AXIS_NUMBERS</span><span class="p">:</span>
<span class="gp">   .....:</span>       <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s2">&quot;invalid axis [</span><span class="si">%s</span><span class="s2">] for alias [</span><span class="si">%s</span><span class="s2">]&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">axis</span><span class="p">,</span> <span class="n">alias</span><span class="p">))</span>
<span class="gp">   .....:</span>    <span class="n">cls</span><span class="o">.</span><span class="n">_AXIS_ALIASES</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">alias</span><span class="p">,</span><span class="bp">None</span><span class="p">)</span>
<span class="gp">   .....:</span> 
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [193]: </span><span class="n">set_axis_alias</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">,</span><span class="s1">&apos;columns&apos;</span><span class="p">,</span> <span class="s1">&apos;myaxis2&apos;</span><span class="p">)</span>

<span class="gp">In [194]: </span><span class="n">df2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">2</span><span class="p">),</span><span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;c1&apos;</span><span class="p">,</span><span class="s1">&apos;c2&apos;</span><span class="p">],</span><span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;i1&apos;</span><span class="p">,</span><span class="s1">&apos;i2&apos;</span><span class="p">,</span><span class="s1">&apos;i3&apos;</span><span class="p">])</span>

<span class="gp">In [195]: </span><span class="n">df2</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="s1">&apos;myaxis2&apos;</span><span class="p">)</span>
<span class="gr">Out[195]: </span>
<span class="go">i1   -0.573143</span>
<span class="go">i2   -0.161663</span>
<span class="go">i3    0.264035</span>
<span class="go">dtype: float64</span>

<span class="gp">In [196]: </span><span class="n">clear_axis_alias</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">,</span><span class="s1">&apos;columns&apos;</span><span class="p">,</span> <span class="s1">&apos;myaxis2&apos;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="creating-example-data">
<h2><span class="yiyi-st" id="yiyi-303">Creating Example Data</span></h2>
<p><span class="yiyi-st" id="yiyi-304">要从某些给定值的每个组合（如R的<code class="docutils literal"><span class="pre">expand.grid()</span></code>函数）创建数据帧，我们可以创建一个dict，其中键是列名，值是数据值的列表：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [197]: </span><span class="k">def</span> <span class="nf">expand_grid</span><span class="p">(</span><span class="n">data_dict</span><span class="p">):</span>
<span class="gp">   .....:</span>    <span class="n">rows</span> <span class="o">=</span> <span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="o">*</span><span class="n">data_dict</span><span class="o">.</span><span class="n">values</span><span class="p">())</span>
<span class="gp">   .....:</span>    <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="o">.</span><span class="n">from_records</span><span class="p">(</span><span class="n">rows</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="n">data_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
<span class="gp">   .....:</span> 

<span class="gp">In [198]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">expand_grid</span><span class="p">(</span>
<span class="gp">   .....:</span>    <span class="p">{</span><span class="s1">&apos;height&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">60</span><span class="p">,</span> <span class="mi">70</span><span class="p">],</span>
<span class="gp">   .....:</span>     <span class="s1">&apos;weight&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">100</span><span class="p">,</span> <span class="mi">140</span><span class="p">,</span> <span class="mi">180</span><span class="p">],</span>
<span class="gp">   .....:</span>     <span class="s1">&apos;sex&apos;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&apos;Male&apos;</span><span class="p">,</span> <span class="s1">&apos;Female&apos;</span><span class="p">]})</span>
<span class="gp">   .....:</span> 

<span class="gp">In [199]: </span><span class="n">df</span>
<span class="gr">Out[199]: </span>
<span class="go">       sex  weight  height</span>
<span class="go">0     Male     100      60</span>
<span class="go">1     Male     100      70</span>
<span class="go">2     Male     140      60</span>
<span class="go">3     Male     140      70</span>
<span class="go">4     Male     180      60</span>
<span class="go">5     Male     180      70</span>
<span class="go">6   Female     100      60</span>
<span class="go">7   Female     100      70</span>
<span class="go">8   Female     140      60</span>
<span class="go">9   Female     140      70</span>
<span class="go">10  Female     180      60</span>
<span class="go">11  Female     180      70</span>
</pre></div>
</div>
</div>
